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)

In reply to Re: How to get 2nd highest value from an array? by jeffa
in thread How to get 2nd highest value from an array? by rsiedl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.