in reply to Re^5: index of the minimum element of the array
in thread index of the minimum element of the array
Better pass the list as a closed over array (here @x)
DB<109> @a= map { int rand 100 } 1..10 => (96, 10, 99, 9, 43, 8, 20, 85, 42, 26) DB<110> sub minindex { my @x=@_; reduce { $x[$a] < $x[$b] ? $a : $b } 0 .. $#_ } DB<111> print minindex @a 5
or even better like ikegami now suggested as ref to avoid overhead
DB<114> @a= map { int rand 100 } 1..10 => (22, 15, 27, 44, 70, 85, +74, 93, 96, 14) DB<115> sub minindex { my $x=\@_; reduce { $x->[$a] < $x->[$b] ? $a : $b } 0 .. $#_ } DB<116> print minindex @a 9
Cheers Rolf
( addicted to the Perl Programming Language)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^7: index of the minimum element of the array
by ikegami (Patriarch) on Jan 17, 2014 at 18:48 UTC |