Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
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 imbibing at the Monastery: (7)
As of 2014-12-22 05:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (110 votes), past polls