Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

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

by Marshall (Prior)
on Sep 25, 2013 at 10:00 UTC ( #1055642=note: print w/ replies, xml ) Need Help??


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

I think you want the peg counts (3rd printout below), but I showed some other stuff just for fun.

#!usr/bin/perl -w use strict; #http://perlmonks.org/?node_id=1055552 my %hash = ( key1 => 10, key3 => 3, key2 => 10, key4 => 5, key5 => 10, key6 => 3, ); # The keys don't matter. # What matters is the histogram of the values. my %histo; #histogram of each key's summary data my %pegs; #peg count's of each key #a bit brain boggling, but it works for a sum... #the (my $value's) are pre-computed at start of the loop # foreach my $value (values %hash) { $pegs{$value}++; $histo{$value}+=$value; } # # various print formats... # print "histo sort by key:\n"; foreach my $x (sort {$a <=> $b} keys %histo) { print "$x => $histo{$x}\n"; } print "\nhisto sort by inverse value:\n"; foreach my $x (sort {$histo{$b} <=> $histo{$a}} keys %histo) { print "$x => $histo{$x}\n"; } print "\nPeg counts of values\n"; foreach my $x (sort {$pegs{$a} <=> $pegs{$b}} keys %pegs) { print "$x => $pegs{$x}\n"; } =summary of prints: sort by key of sums: 3 => 6 #keys 3,6 5 => 5 #keys 5 10 => 30 #keys 1,2,5 sort by inverse value of sums: (note swap of $b and $a) #I did that to make it a bit more interesting... 10 => 30 3 => 6 5 => 5 Peg counts of values: (value and number of times seen) 5 => 1 3 => 2 10 => 3 =cut
There are a number of ways to present the max (eg 10 => 3) peg count, but what do you want if say 3=>3 also?


Comment on Re: How to compare hash values within the same hash?
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (9)
As of 2015-07-03 13:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (53 votes), past polls