Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

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

by BillKSmith (Deacon)
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 cooling their heels in the Monastery: (8)
As of 2015-07-29 21:05 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 (269 votes), past polls