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

Re: Counting Data in two columns?

by neniro (Priest)
on Jun 15, 2006 at 08:06 UTC ( #555442=note: print w/ replies, xml ) Need Help??

in reply to Counting Data in two columns?

You can use a HoH for this:

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %fruits; while (<DATA>) { chomp; my @row = split /\s+/, $_, 2; warn @row and next unless @row == 2; $fruits{$row[0]}->{$row[1]}++; } for my $fruit (sort keys %fruits) { my $sum = 0; $sum += $fruits{$fruit}->{$_} for keys %{$fruits{$fruit}}; $sum = $sum == 1 ? $sum . ' TIME ' : $sum . ' TIMES '; my $people = ''; my @people = keys %{$fruits{$fruit}}; for (0..$#people) { $people .= $_ != $#people ? uc ($people[$_]) . ' and ' : uc ($people[$_]); } print uc $fruit, ' appears ', $sum, 'associated with ', $people, " +\n"; } __DATA__ apple Tom orange Tom pear Tom mango David apple David

Comment on Re: Counting Data in two columns?
Download Code

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (11)
As of 2014-07-22 09:27 GMT
Find Nodes?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:

    Results (109 votes), past polls