#!perl
#
# farm.pl
use strict;
use warnings;
# Lookup table of farm animals by home
my %home_of = (
Cat => 'House',
Cow => 'Field',
Dog => 'Yard',
Goat => 'Barn',
Hamster => 'House',
Hen => 'Coop',
Horse => 'Barn',
Sheep => 'Barn',
Pig => 'Sty',
Rooster => 'Yard',
);
my %animals_by;
# Add each animal to a list of farm animals by home...
while (my ($animal, $home) = each %home_of) {
push @{ $animals_by{$home} }, $animal;
}
# Print ordered lists of animals by an ordered list of homes...
for my $home (sort keys %animals_by) {
my $list_of_animals = join ', ', sort @{ $animals_by{$home} };
print "$home\t$list_of_animals\n";
}
__END__
Barn Goat, Horse, Sheep
Coop Hen
Field Cow
House Cat, Hamster
Sty Pig
Yard Dog, Rooster
|