-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3-2.pl
50 lines (40 loc) · 1.23 KB
/
3-2.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#3-2:
use strict;
use warnings;
require 5.010.0;
use feature "state";
my @skipper = qw{ blue_shirt hat jacket preserver sunscreen};
my @professor = qw { sunscreen water_bottle slide_rule batteries radio};
my @gilligan = qw { red_shirt hat lucky_socks water_bottle };
my %all = (
"Skipper" => \@skipper,
"Professor" => \@professor,
"Gilligan" => \@gilligan,
);
sub check_required_items {
my $who = shift;
my $items = shift;
my %items = map { $_, 1} @$items;
#state @required = qw(preserver sunscreen water_botle jacket); # state requires 5.10. (similar to static, initialized the first time only.)
#Initialization of state variables in list context currently forbidden
my @required = qw(preserver sunscreen water_botle jacket); # state requires 5.10. (similar to static, initialized the first time only.)
my @missing = ();
foreach my $item (@required) {
unless (exists $items{$item}) {
print "$who is missing $item. \n";
push @missing, $item;
}
}
if (@missing) {
print "Adding @missing to @$items for $who.\n";
push @$items, @missing;
}
}
sub check_items_for_all {
my $hash_ref = shift;
my %hash = %$hash_ref;
foreach my $name (keys %hash) {
check_required_items($name, $hash{$name});
}
}
check_items_for_all(\%all);