in reply to How to retrieve the worst value?

Turn the hash inside-out, then sort the keys in descending order and pick the first.

my %hash = (processone => 1, processtwo => 0.005, processthree => 3) +; my %hash2 = map {$hash{$_} => $_} keys %hash; my $worst = (reverse sort keys %hash2)[0]; print "the worst process is $hash2{$worst} with a time of $worst min +utes\n";

Replies are listed 'Best First'.
Re: Re: How to retrieve the worst value?
by halley (Prior) on Apr 01, 2004 at 16:07 UTC
    Why all the excess complexity? sort { $h{$a} <=> $h{$b} } should be just fine here.
    %hash = (processone => 1, processtwo => 0.005, processthree => 3); @_ = sort { $hash{$a} <=> $hash{$b} } keys %hash; print $_[0], ' => ', $hash{$_[0]}, " (lowest)\n"; print $_[-1], ' => ', $hash{$_[-1]}, " (highest)\n";

    --
    [ e d @ h a l l e y . c c ]

Re: Re: How to retrieve the worst value?
by mce (Curate) on Apr 01, 2004 at 14:13 UTC
    Hi,

    Why create an extra hash?

    print +(sort { $a <=> $b } values %hash)[-1];
    The sort by default sorts alfanumeric, so in your case 30 is better than 4.

    ---------------------------
    Dr. Mark Ceulemans
    Senior Consultant
    BMC, Belgium