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};
In reply to Re: grep surprise
by ikegami
in thread grep surprise
by morgon
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |