Blob


1 #!/usr/bin/perl
3 # Using the final version of check_required_items, write a subroutine
4 # check_items_for_all that takes as its only parameter a reference to a
5 # hash whose keys are the people aboard the Minnow and whose corresponding
6 # values are array references of the things they intend to bring on board.
7 #
8 # For example, the hash reference might be constructed like so:
9 #
10 # my @gilligan = (... gilligan items ...);
11 # my @skipper = (... skipper items ...);
12 # my @professor = (... professor items ...);
13 #
14 # my %all = (
15 # Gilligan => \@gilligan,
16 # Skipper => \@skipper,
17 # Professor => \@professor,
18 # );
19 #
20 # check_items_for_all(\%all);
21 #
22 # The newly constructed subroutine should call check_required_items for
23 # each person in the hash, updating their provisions list to include the
24 # required items.
25 #
26 # Some starting code is in the Downloads section on
27 # https://www.intermediateperl.com/
29 use v5.24;
30 use warnings;
31 use strict;
32 use utf8;
34 sub check_required_items {
35 my $who = shift;
36 my $items = shift;
37 my %whose_items = map { $_, 1 } @$items;
38 my @required = qw(preserver sunscreen water_bottle jacket);
39 my @missing = ();
41 for my $item (@required) {
42 unless ($whose_items{$item}) {
43 print "$who is missing $item.\n";
44 push @missing, $item;
45 }
46 }
48 if (@missing) {
49 print "Adding @missing to @$items for $who.\n";
50 push @$items, @missing;
51 }
52 }
53 sub check_items_for_all {
54 my $allref = shift;
55 foreach my $person (keys %$allref) {
56 check_required_items($person, $allref->{$person});
57 }
58 }
59 my @gilligan = qw(red_shirt hat lucky_socks water_bottle);
60 my @skipper = qw(blue_shirt hat jacket preserver sunscreen);
61 my @professor = qw(sunscreen water_bottle slide_rule batteries radio);
63 my %all = (
64 Gilligan => \@gilligan,
65 Skipper => \@skipper,
66 Professor => \@professor,
67 );
69 check_items_for_all(\%all);