Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
Perl: the Markov chain saw
 
PerlMonks  

Re: How to get 2nd highest value from an array?

by GrandFather (Saint)
on Dec 13, 2005 at 20:39 UTC ( [id://516494]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to How to get 2nd highest value from an array?

Non-sorted hash values or array compatible version:

use warnings; use strict; my %hash = (4 => '1', 5 => '5', 7 => '2', 1 => '7', 10 => '9', 3 => '0 +', 2 => '3', 9 => '8'); print GetSecondLargest(values %hash); sub GetSecondLargest { my @largest = (shift); while (@_) { my $new = shift; if ($new >= $largest [-1]) { unshift @largest, $new; next; } $largest[1] = $new if ! $#largest or $new > $largest[1]; } return $#largest ? $largest[1] : undef; }

Prints:

8

DWIM is Perl's answer to Gödel

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://516494]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.