For example, iterate over the data for the Skipper, Gilligan, and the
Professor by first building a larger data structure holding the
entire list of provision lists:
my @skipper = qw(blue_shirt hat jacket preserver sunscreen);
my @skipper_with_name = ("Skipper", \@skipper);
my @professor = qw(sunscreen water_bottle slide_rule batteries radio);
my @professor_with_name = ("Professor", \@professor);
my @gilligan = qw(red_shirt hat lucky_socks water_bottle);
my @gilligan_with_name = ("Gilligan", \@gilligan);
How do you call the existing check_required_items(
) with this data structure? The following code is easy
enough.
for my $person (@all_with_names) {
my $who = $$person[0];
my $provisions_reference = $$person[1];
check_required_items($who, $provisions_reference);
}
This requires no changes to the subroutine.
$person will be each of
$all_with_names[0],
$all_with_names[1], and
$all_with_names[2], as the loop progresses. When
you dereference $$person[0], you get
"Skipper,"
"Professor," and
"Gilligan," respectively.
$$person[1] is the corresponding array reference
of provisions for that person.
Of course, you can shortcut this as well, since the entire
dereferenced array matches the argument list precisely:
for my $person (@all_with_names) {
check_required_items(@$person);
}
or even:
check_required_items(@$_) for @all_with_names;