Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: extracting the largest values

by Perl Mouse (Chaplain)
on Nov 22, 2005 at 11:26 UTC ( #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? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2022-12-01 13:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?