#!/usr/bin/perl -w use warnings; use strict; my @animals = ("cat", "dog", "fish", "horse", "pig"); # Hash to map an animal to an array index. I'm sure there's a # better way, but I don't know it... my %a2i; my $i=0; for my $a (@animals) { $a2i{$a}=$i++; } my @count; while () { my ($x,$y) = split /\s/; ($x,$y) = ($y,$x) if ($x gt $y); $count[$a2i{$x}][$a2i{$y}]++; } for my $x (sort keys %a2i) { next if !defined $count[$a2i{$x}]; for my $y (sort keys %a2i) { next if !defined $count[$a2i{$x}][$a2i{$y}]; print $count[$a2i{$x}][$a2i{$y}], " people have both a ", $x, " and a ", $y, "\n"; } } __DATA__ cat dog cat fish pig dog horse fish pig fish dog fish fish cat