in reply to find position of element of array

TIMTOWDI, and List::MoreUtils offers another solution:
use List::MoreUtils qw(firstidx); my @array = ("a","b","c","d"); my $index = firstidx { $_ eq "c" } @array; my $wanted = $array[$index - 1];

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -- Brian W. Kernighan

Replies are listed 'Best First'.
Re^2: find position of element of array
by Perl Mouse (Chaplain) on Nov 07, 2005 at 10:43 UTC
    Note that this code will assign "d" to $wanted if searching with "a".
    Perl --((8:>*