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

Re: Count number of elements in HoH and sum them for other keys

by BillKSmith (Priest)
on Jun 03, 2014 at 12:33 UTC ( #1088407=note: print w/replies, xml ) Need Help??


in reply to Count number of elements in HoH and sum them for other keys

A simple hash is all you need. Use the things you want to count as keys.
use strict; use warnings; my %hash; while (<DATA>) { my @elements = split; $hash{join ' => ', @elements[0,1]}++; $hash{$elements[0]}++; } foreach my $key (sort keys %hash) { printf "%-6s => %d\n", $key, $hash{$key}; } __DATA__ A B n1 A B n2 A C n1 D E n2 D E n4 D F n1
OUTPUT:
A => 3 A => B => 2 A => C => 1 D => 3 D => E => 2 D => F => 1
Bill

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1088407]
help
Chatterbox?
[MidLifeXis]: corelist Typo. Wow. Time to go, fingers are malfunctioning.
[marioroy]: Thanks MidLifeXis. Leaned another thing about Perl. Wow.
[marioroy]: Didn't know of corelist until today. Cool.
[marioroy]: Lady_Aleena Which module?

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (11)
As of 2017-05-24 18:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?