in reply to Simple things should be coded simply (Re: Idiomatic Array Index Search?)
in thread Idiomatic Array Index Search?
I don't like code like this because you've got two things in that loop that are being kept in sync only coincidentally: the iteration of the foreach, and the incrementing of the index. I'd let there be one thing, and use the indirection to ensure consistency:sub get_index (\@$) { my ($a, $e) = @_; my $i = 0; for (@$a) { return $i if $e eq $_; $i++; } return -1; }
This is clearer to me. Simple things should be coded simply. {grin}sub get_index (\@$) { my ($a, $e) = @_; for (my $i = 0; $i <= $#$a; $i++) { return $i if $e eq $a->[$i]; } return -1; }
-- Randal L. Schwartz, Perl hacker
|
|---|