in reply to printing largest hash value

#!/usr/bin/perl my %hash = ( red => 2, pink => 1, orange => 4, black => 3, blue => 4 ); foreach ( reverse sort { $b <=> $a } keys %hash ) { if ( !$highest || $hash{$_} == $highest ) { print "\t$_, $hash{$_}\n"; $highest = $hash{$_}; } }

Replies are listed 'Best First'.
Re^2: printing largest hash value
by tomhukins (Curate) on Feb 22, 2005 at 09:40 UTC

    Avoid rearranging the elements in an array you've just sorted.

    Instead of reverse sort { $b <=> $a } keys %hash, write sort { $a <=> $b } keys %hash. The second approach reads more clearly and runs more quickly.