Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: printing largest hash value

by RiotTown (Scribe)
on Feb 22, 2005 at 03:33 UTC ( [id://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?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-04-20 00:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found