in reply to find position of element of array

To find the index of an element within an array, you can grep.

my ($index) = grep { $array[$_] eq 'c' } 0..$#array;

Then just subtract one from $index. (Watch out of $index happens to be zero though.)

Update: Forgot list context for $index.

Replies are listed 'Best First'.
Re^2: find position of element of array
by spatterson (Pilgrim) on Nov 07, 2005 at 12:26 UTC
    Wow, and all this time I've been doing variations of
    for (my $i = 0; $i < @foo; ++$i) { if ($foo[$i] eq $match_element) { return $i; } }
    just another cpan module author