Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

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

by BillKSmith (Chaplain)
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


Comment on Re: Count number of elements in HoH and sum them for other keys
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (9)
As of 2014-11-26 12:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (170 votes), past polls