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
Perl assignment solved using array and hash
Assign the hash variable called car_catalog to include the following car models and their showroom prices in dollars. Use the car model name as the hash key. The cars and prices are:
Model: BMW Series 5, price: 100000
Model: Mercedes 2000, price: 250000
Model: Toyota Corolla, price: 20000
Model: Lexus 3, price: 95000
Assign an array variable called my_wishlist with the two cars you want to buy: the first array element is the full model name of the BMW car and the second array model is the full model name of the Toyota car. Use the array variable contents as keys to the hash variable in order to print lines in the following format: "I would like to buy one for the price of Dollars."
Solution
%car_catalog = ("BMW Series 5" => 100000 , "Mercedes 2000" => 250000, "Toyota Corolla" => 20000,"Lexus 3"=>95000);
@my_wishlist = ("BMW Series 5","Toyota Corolla");
print "I would like to buy one $my_wishlist[0] for the price of $car_catalog{$my_wishlist[0]} Dollars.\n";
print "I would like to buy one $my_wishlist[1] for the price of $car_catalog{$my_wishlist[1]} Dollars.\n";
Source:https://www.learn-perl.org/en/Variables_and_Types