in reply to grep surprise

grep in scalar context returns the number of matches. To get the first match, you can use the following:

my ($i1) = grep { $array[$_] == 1 } 0..$#array; my ($i2) = grep { $array[$_] == 2 } 0..$#array; my ($i3) = grep { $array[$_] == 3 } 0..$#array;

Might be worth creating a lookup table.

my %lookup = map { $array[$_] => $_ } 0..$#array; my $i1 = $lookup{1}; my $i2 = $lookup{2}; my $i3 = $lookup{3};

Replies are listed 'Best First'.
Re^2: grep surprise
by Eily (Monsignor) on Nov 12, 2018 at 09:39 UTC

    The second one returns that last index though (because if there are duplicates, each new occurence overwrites the previous one). This is not an issue of course if the values are unique, in which case it might make sense to store them in a hash in the first place.