in reply to Re^3: 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?
$idx = $arr[0]; for(0..$#arr) { $idx = $_ if( ($arr[$idx]<0) || (($arr[$_] >= 0) && ($arr[$_] +< $arr[$idx])) ) }
This returns strange results for certain corner cases:
I'd hate to have to extend this to dealing with fractional numbers. The O(n) solutions here and here both return an unambiguous undef for lists containing no LNN, including empty lists.c:\@Work\Perl\monks>perl -wMstrict -le "use Data::Dump qw(pp); ;; for my $ar ( [ ], [ -1 ], [ -99 ], [ 0, -99 ], [ -99, 0 ], ) { my $idx = $ar->[0]; for (0..$#$ar) { $idx = $_ if( ($ar->[$idx]<0) || (($ar->[$_] >= 0) && ($ar->[$_] +< $ar->[$idx])) ); } print qq{(@$ar) -> lnn index of }, pp $idx; } " () -> lnn index of undef (-1) -> lnn index of 0 Use of uninitialized value in numeric lt (<) at -e line 1. (-99) -> lnn index of -99 (0 -99) -> lnn index of 0 Use of uninitialized value in numeric lt (<) at -e line 1. Use of uninitialized value in numeric lt (<) at -e line 1. Use of uninitialized value in numeric lt (<) at -e line 1. (-99 0) -> lnn index of -99
Give a man a fish: <%-{-{-{-<
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: How to get the index of smallest whole number in an array?
by bliako (Abbot) on Jul 03, 2018 at 00:02 UTC |