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

Re: extracting the largest values

by Perl Mouse (Chaplain)
on Nov 22, 2005 at 11:26 UTC ( [id://510728]=note: print w/replies, xml ) Need Help??


in reply to extracting the largest values

Your problem is that if you find a new largest value, you completely forget the current largest value. Instead the current largest value should become the new second largest value, and the new largest value should become the new largest value. Something like (untested):
if ($value > $largest_value) { ($largest_value, $second_value) = ($value, $largest_value); next; } if ($value > $second_value) { $second_value = $value; }
If you go for a minimum amount of lines, you could also do:
($largest_value, $second_value) = sort {$a <=> $b} $largest_value, + $second_value, $value;
Perl --((8:>*

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-26 02:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found