Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^6: index of the minimum element of the array

by LanX (Canon)
on Jan 17, 2014 at 18:40 UTC ( #1071001=note: print w/ replies, xml ) Need Help??


in reply to Re^5: index of the minimum element of the array
in thread index of the minimum element of the array

Ikegami is right, thats not working!

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

update

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)


Comment on Re^6: index of the minimum element of the array
Select or Download Code
Re^7: index of the minimum element of the array
by ikegami (Pope) on Jan 17, 2014 at 18:48 UTC
    Don't copy @_, take a reference to it.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2014-11-23 13:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (132 votes), past polls