Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
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 drinking their drinks and smoking their pipes about the Monastery: (12)
As of 2015-07-29 19:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (267 votes), past polls