Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: index of the minimum element of the array

by ikegami (Pope)
on Jan 17, 2014 at 18:14 UTC ( #1070997=note: print w/ replies, xml ) Need Help??


in reply to index of the minimum element of the array

use List::Util qw( reduce ); sub min_idx { my ($ar) = @_; return reduce { $ar->[$b] > $ar->[$a] ? $b : $a } 0..$#$ar; }
which is equivalent to the cheaper
sub min_idx { my ($ar) = @_; return undef if !@$ar; my $idx = 0; for (1..$#$ar) { $idx = $_ if $arr->[$_] > $arr->[$idx]; } return $idx; }

Usage:

my $min_idx = min_index(\@a);


Comment on Re: index of the minimum element of the array
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1070997]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2014-10-26 07:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (152 votes), past polls