Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

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

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 making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2016-08-31 08:17 GMT
Find Nodes?
    Voting Booth?
    The best thing I ever won in a lottery was:

    Results (427 votes). Check out past polls.