Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: printing largest hash value

by RiotTown (Scribe)
on Feb 22, 2005 at 03:33 UTC ( #433235=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://433235]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (2)
As of 2021-12-02 15:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (22 votes). Check out past polls.

    Notices?