Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (14)
As of 2015-07-01 16:31 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 (11 votes), past polls