An array @family holds a list of family member names. The first hash %shoe_color contains favorite shoe color per person name. The second hash %shoe_size contains shoe size per person name.
Evaluate and print the favorite shoe color and shoe size per each family member. For shoe sizes 10 and above, add the word 'large' to the output line.
Output lines should be in the format: "Homer wears large brown shoes size 12".
Note: not all family members may be included in the hash variables, so you better conditionally check if they exist or not (using the exists operator). If a name does not exist, add the key/value pair into the hash variables - for show color add: black; for shoe size add 99.
Solution
@family = ('Homer', 'Moe', 'Maggie');
%shoe_color = ('Lisa' => 'red', 'Homer' => 'brown', 'Maggie' => 'pink', 'Marge' => 'blue', 'Bart' => 'yellow');
%shoe_size = ('Moe' => 9, 'Lisa' => 7, 'Homer' => 12, 'Bart' => 8, 'Maggie' => 4);
$default_shoe_color = "black";
$default_shoe_size = 4;
$member = $family[0];
if (!exists $shoe_color{$member}) {
$shoe_color{$member} = $default_shoe_color;
}
if (!exists $shoe_size{$member}) {
$shoe_size{$member} = $default_shoe_size;
}
$is_large = ($shoe_size{$member} >= 10) ? " large " : " ";
print "$member wears$is_large$shoe_color{$member} shoes size $shoe_size{$member}\n";
$member = $family[1];
if (!exists $shoe_color{$member}) {
$shoe_color{$member} = $default_shoe_color;
}
if (!exists $shoe_size{$member}) {
$shoe_size{$member} = $default_shoe_size;
}
$is_large = ($shoe_size{$member} >= 10) ? " large " : " ";
print "$member wears$is_large$shoe_color{$member} shoes size $shoe_size{$member}\n";
$member = $family[2];
if (!exists $shoe_color{$member}) {
$shoe_color{$member} = $default_shoe_color;
}
if (!exists $shoe_size{$member}) {
$shoe_size{$member} = $default_shoe_size;
}
$is_large = ($shoe_size{$member} >= 10) ? " large " : " ";
print "$member wears$is_large$shoe_color{$member} shoes size $shoe_size{$member}\n";
Sunday, October 30, 2022
Perl Assignment solved using array and hash
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment