Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

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 taking refuge in the Monastery: (5)
As of 2016-02-10 06:08 GMT
Find Nodes?
    Voting Booth?

    How many photographs, souvenirs, artworks, trophies or other decorative objects are displayed in your home?

    Results (332 votes), past polls