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

Re: How to compare hash values within the same hash?

by LanX (Chancellor)
on Sep 24, 2013 at 23:53 UTC ( #1055566=note: print w/replies, xml ) Need Help??


in reply to How to compare hash values within the same hash?

DB<100> %hash = ( key1 => 10, key2 => 10, key3 => 3, key4 => 5, key5 => 10 ); => ("key1", 10, "key2", 10, "key3", 3, "key4", 5, "key5", 10) DB<101> $count{$_}++ for values %hash => "" DB<102> \%count => { 3 => 1, 5 => 1, 10 => 3 } DB<106> ($max) = reverse sort values %count => 3 DB<109> grep { $count{$_} == $max } %count => 10

this will give you a list of all values which are maximal, not only one.

update

otherwise, if only one value is enough:

DB<119> %rcount= reverse %count => (3, 10, 3, 10) DB<120> \%rcount => { 1 => 3, 3 => 10 } DB<121> $rcount{$max} => 10

and here an easier approach to find the maxmimum.

DB<113> use List::Util qw/max/ DB<114> $max = max values %count => 3

Cheers Rolf

( addicted to the Perl Programming Language)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2016-10-01 21:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?






    Results (8 votes). Check out past polls.