Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Counting but with two columns

Status
Not open for further replies.

sdslrn123

Technical User
Jan 17, 2006
78
GB
Code:
#!/usr/bin/perl

@foo= ("apple","orange","pear","pear","mango","apple");

for (@foo) {
$bar{$_}++;
}

while ( ($k,$v) = each %bar ) {
if ($v =~ 1){
    print "$k appears $v time\n";
}
elsif ($v !=~ 1){
    print "$k appears $v times\n";
}
}

The above code works nicely with a one column set of data:
Code:
apple
orange
pear
pear
mango
apple

For me the next stage is working with a 2-column set of data:
Code:
apple  Tom
orange Tom
pear   Tom
mango  David
apple  David

How would I go about creating a program that outputs:
Code:
APPLE appears 2TIMES associated with TOM and DAVID
ORANGE appears 1TIME associated with TOM
...e.t.c

I am hoping to extend this to data that has lots of rows of this type of 2 column data. My thinking is playing around with hashes?
 
Yes, hashes are a good thing. Store the information like this:

$counts{$fruit}->{$person} +=1;


If this is school work, I will leave it to you to fill in the details and figure out how to dereference. If you can convince me it is for legitimate purposes, then I have the whole program written and ready to go.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top