in reply to max key-value pair in a hash

#! perl -slw use strict; use List::Util qw[ max ]; my %some_hash = ( sunny => .63, cloudy => .17, rainy => .2 ); my $max = max values %some_hash; print +{ reverse %some_hash }->{ $max }, ' :: ', $max; __END__ C:\test>539366 sunny :: 0.63

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: max key-value pair in a hash
by kettle (Beadle) on Mar 27, 2006 at 06:26 UTC
    thanks for the replies! all very helpful. I think that this last was what I was most curious about though - how to print the key, given the value. but what happens where there are two keys that share the same value? and what is the 'l' option in the shebang ??
      but what happens where there are two keys that share the same value?

      You'll get the weather associated with one of the maximal values--which one is determined by the hashing algorithm and the text of the keys.

      This is exactly the same effect as with your sort sort method.

      and what is the 'l' option in the shebang ??

      It sets $/ & $\ (INPUT and OUTPUT SEPARATORS) to \n. It just saves having to type  . "\n"; each time. One downside is that if you use a module that uses "\n" explicitly when printing stuff, it ends up double spaced, but it's useful for standalone scripts.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.