in reply to max "value " of an hash

List::Util::reduce() is very useful for this:

use List::Util qw[ shuffle reduce ];; my %hash; @hash{ 'a'..'z' } = shuffle 1 .. 26;; my $maxKey = reduce{ $hash{ $a } > $hash{ $b } ? $a : $b } keys %hash; +; print "$maxKey : ", $hash{ $maxKey };; x : 26

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"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 "value " of an hash
by Aaronrp (Scribe) on Jan 18, 2011 at 19:11 UTC

    Of course, List::Util also comes with max() and maxstr():

    #!perl use 5.010; use strict; use warnings; no warnings 'numeric'; # otherwise max() gives a warning on keys "a" and "b" my %hash = (a => 'value' , b => 'value', 0 => 'value', 1 => 'value'); # the values don't matter for this example use List::Util (qw/max maxstr/); my $max = max (keys %hash); my $maxstr = maxstr (keys %hash); say "numeric: $max\tstring: $maxstr";

    returns

    numeric: 1 string: b