Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Answer: How can I find the index of the biggest element in an array?

by Jenda (Abbot)
on May 28, 2007 at 14:09 UTC ( #617843=note: print w/ replies, xml ) Need Help??


in reply to Answer: How can I find the index of the biggest element in an array?
in thread arrays

I don't think it's good to scare people with a ternary operator and statement modifier in one statement. I do think

while ($i--) { $max = $i if $data[$i] > $data[$max]; }
would be much more readable. I'd probably write it like this though:
my $max = 0; for (0 .. $#data) { $max = $_ if $data[$_] > $data[$max] }

Update (suggested by ysth): It's actually better to start looping at index 1:

my $max = 0; for (1 .. $#data) { $max = $_ if $data[$_] > $data[$max] }
There's no point in comparing the first element to itself.


Comment on Re: Answer: How can I find the index of the biggest element in an 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://617843]
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: (5)
As of 2015-07-05 21:41 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 (68 votes), past polls