!perl
my $input = [
{
Model => "Aggressor",
Manufacturer => "Shoe Co.",
Size => "10"
},
{
Model => "LookinGood",
Manufacturer => "Shoe Co.",
Size => "12"
},
{
Model => "PowerStrike",
Manufacturer => "Footware Inc.",
Size => "7"
},
{
Model => "RunFast",
Manufacturer => "Footware Inc.",
Size => "6"
},
{
Model => "RunFast",
Manufacturer => "Footware Inc.",
Size => "8"
},
{
Model => "Lounger",
Manufacturer => "Comfy Shoe Ltd.",
Size => "9"
},
{
Model => "Lounger",
Manufacturer => "Comfy Shoe Ltd.",
Size => "7"
},
];
my %trainers;
foreach my $trainer ( @{ $input } )
{
foreach my $f ( qw{ Model Manufacturer } )
{
$trainers{ $trainer -> {"Model"} } -> { $f } =
$trainer->{$f};
}
$trainers{ $trainer -> {"Model"} } -> {"Size"}
-> { $trainer -> {"Size"} } = undef;
}
while ( my ( $k,$v ) = each ( %trainers ) )
{
print "Trainer: ".$v->{"Manufacturer"}." ".$k.
" is available in the following sizes:\n";
my @sizes = keys %{ $v -> {"Size"} } ;
print "\t". join " , ",@sizes;
print "\n";
}
1) What does the "qw" operator do?
2) What is the effect of the nested loop?
3) What do you anticipate the output of the program to be?
4) What would happen if "Shoe Co." were to release a trainer called
"PowerStrike", and the data stored in $input updated to reflect this?
my $input = [
{
Model => "Aggressor",
Manufacturer => "Shoe Co.",
Size => "10"
},
{
Model => "LookinGood",
Manufacturer => "Shoe Co.",
Size => "12"
},
{
Model => "PowerStrike",
Manufacturer => "Footware Inc.",
Size => "7"
},
{
Model => "RunFast",
Manufacturer => "Footware Inc.",
Size => "6"
},
{
Model => "RunFast",
Manufacturer => "Footware Inc.",
Size => "8"
},
{
Model => "Lounger",
Manufacturer => "Comfy Shoe Ltd.",
Size => "9"
},
{
Model => "Lounger",
Manufacturer => "Comfy Shoe Ltd.",
Size => "7"
},
];
my %trainers;
foreach my $trainer ( @{ $input } )
{
foreach my $f ( qw{ Model Manufacturer } )
{
$trainers{ $trainer -> {"Model"} } -> { $f } =
$trainer->{$f};
}
$trainers{ $trainer -> {"Model"} } -> {"Size"}
-> { $trainer -> {"Size"} } = undef;
}
while ( my ( $k,$v ) = each ( %trainers ) )
{
print "Trainer: ".$v->{"Manufacturer"}." ".$k.
" is available in the following sizes:\n";
my @sizes = keys %{ $v -> {"Size"} } ;
print "\t". join " , ",@sizes;
print "\n";
}
1) What does the "qw" operator do?
2) What is the effect of the nested loop?
3) What do you anticipate the output of the program to be?
4) What would happen if "Shoe Co." were to release a trainer called
"PowerStrike", and the data stored in $input updated to reflect this?