Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
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 musing on the Monastery: (9)
As of 2014-12-23 00:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (133 votes), past polls