in reply to How to get the index of smallest whole number in an array?
List::Util::reduce()
my @array = ('3','4','71','1', '-598', '-100203'); print reduce{ $a = $b if $array[$b] >=0 and $array[$b] < $array[$a]; $ +a } 0 .. $#array;; 3
Or more simply:
print reduce{ $array[$b] >=0 && $array[$b] < $array[$a] ? $b : $a } 0 +.. $#array;
Bad data -- ie. empty array, or all negatives -- returns undef. Doesn't check values are integer.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How to get the index of smallest whole number in an array? (Simplified)
by Eily (Monsignor) on Jul 06, 2018 at 08:28 UTC |