#!/usr/bin/perl -w use warnings; use strict; my @animals = ("cat", "dog", "fish", "horse", "pig"); my %count; while () { my ($x,$y) = split /\s/; ($x,$y) = ($y,$x) if ($x gt $y); $count{$x}{$y}++; } for my $x (sort keys %count) { for my $y (sort keys %{$count{$x}}) { print $count{$x}{$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