in reply to Re: How to get the index of smallest whole number in an array?
in thread How to get the index of smallest whole number in an array?
It's possible to combine the grep expression with the preceding map expression:
c:\@Work\Perl\monks>perl -wMstrict -le "my @ra = qw(13 4 71 2 -598 -100203); ;; my ($i_lwn) = map $_->[0], sort { $a->[1] <=> $b->[1] } map { $ra[$_] >= 0 ? [ $_, $ra[$_] ] : () } 0 .. $#ra ; ;; if (defined $i_lwn) { print qq{\$ra[$i_lwn] == lwn $ra[$i_lwn]}; } else { print 'no lowest whole number in array'; } " $ra[3] == lwn 2
Update: This approach returns the lowest index of more than one LWN in an array (assuming sort is stable, which it is IIUC the documentation). I'm not sure what the simplest approach to changing this code to obtain the highest index of multiple LWNs would be. I've tested it only partially, but it may be as simple a putting a reverse call in front of the 0 .. $#ra expression at the bottom of the map stack.
Give a man a fish: <%-{-{-{-<
|
|---|