Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
No such thing as a small change
 
PerlMonks  

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

by jeffa (Bishop)
on Dec 13, 2005 at 17:57 UTC ( [id://516466]=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?

Update: Oops, i actually meant to say: Is there anyway to do this same thing on the values of a hash?

Do you mean just the values themselves or the keys based on the values? Here is some code for both cases:

my %hash = ( 4 => 20, # second highest key 2 => 40, # second highest value 3 => 30, 1 => 50, 5 => 10, ); # second highest value print ((sort values %hash)[-2]); # key based on the second highest value print ((sort { $hash{$b} <=> $hash{$a} } keys %hash)[1]);
For the second example, since we already have to specify a sort block, i choose to sort descending and use the subscript 1 and not -2. TIMTOWTDI. :)

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://516466]
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.