3.1. Performing the Same Task on Many Arrays
Before the Minnow can leave on an excursion (e.g., a three-hour
tour), every passenger and crew member should be checked to ensure
they have all the required trip items in their possession.
Let's say that for maritime safety, every person on
board the Minnow needs to have a life preserver, some sunscreen, a
water bottle, and a rain jacket. You can write a bit of code to check
for the Skipper's supplies:
my @required = qw(preserver sunscreen water_bottle jacket);
my @skipper = qw(blue_shirt hat jacket preserver sunscreen);
for my $item (@required) {
unless (grep $item eq $_, @skipper) { # not found in list?
print "skipper is missing $item.\n";
}
}
Of course, if you want to check on Gilligan and the Professor, you
might write the following code:
my @gilligan = qw(red_shirt hat lucky_socks water_bottle);
for my $item (@required) {
unless (grep $item eq $_, @gilligan) { # not found in list?
print "gilligan is missing $item.\n";
}
}
my @professor = qw(sunscreen water_bottle slide_rule batteries radio);
for my $item (@required) {
unless (grep $item eq $_, @professor) { # not found in list?
print "professor is missing $item.\n";
}
}
You may start to notice a lot of repeated code here and decide that
it would be served best in a subroutine:
sub check_required_items {
my $who = shift;
my @required = qw(preserver sunscreen water_bottle jacket);
for my $item (@required) {
unless (grep $item eq $_, @_) { # not found in list?
print "$who is missing $item.\n";
}
}
}
my @gilligan = qw(red_shirt hat lucky_socks water_bottle);
check_required_items("gilligan", @gilligan);
So far, so good. You can check the Skipper and the Professor with
just a bit more code:
my @skipper = qw(blue_shirt hat jacket preserver sunscreen);
my @professor = qw(sunscreen water_bottle slide_rule batteries radio);
check_required_items("skipper", @skipper);
check_required_items("professor", @professor);
And for the other passengers, you repeat as needed. Although this
code meets the initial requirements, you've got two
problems to deal with: