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

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

by BillKSmith (Vicar)
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?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2017-08-20 21:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Who is your favorite scientist and why?



























    Results (317 votes). Check out past polls.

    Notices?