1. Write a program that will ask the user for a given name and report the corre-
ID: 3836342 • Letter: 1
Question
1. Write a program that will ask the user for a given name and report the corre- sponding family name. Use the names of people you know, or (if you spend so much time on the computer that you don't know any actual people) use the fol- lowing table:
Input Output
fred flintstone
barney rubble
Wilma flintstone
What I have so far.
2. Write a program that reads a series of words (with one word per line) until end-of-input, then prints a summary of how many times each word was seen. (Hint: remember that when an undefined value is used as if it were a number, Perl automatically converts it to 0. It may help to look back at the earlier exercise that kept a running total.) So, if the input words were fred, barney, fred, dino, wilma, fred (all on separate lines), the output should tell us that fred was seen 3 times. For extra credit, sort the summary words in code point order in the out- put.
What I have so far.
3. Write a program to list all of the keys and values in %ENV. Print the results in two columns in ASCIIbetical order. For extra credit, arrange the output to verti- cally align both columns. The length function can help you figure out how wide to make the first column. Once you get the program running, try setting some new environment variables and ensuring that they show up in your output. Expert Answer Hang tight! We are finding a Chegg Expert to answer this question.
Explanation / Answer
1.
#! /usr/bin/perl
my %last_name = qw {
fred flintstone
barney rubble
wilma flintstone
};
print("Please enter a first name: ");
chomp(my $name = <STDIN>);
print("That's $name $last_name[$name}. ");
2.
#! /usr/bin/perl
my(@words, %count, $word);
chomp(@words = <STDIN>);
foreach $word (@words) {
$count{$word} += 1;
}
foreach $word (keys %count) {
print("$word was seen $count{$word} times. ");
}
3.
#! /usr/bin/perl
my $longest = 0;
foreach my $key ( keys %ENV ) {
my $key_length = length( $key );
$longest = $key_length if $key_length > $longest;
}
foreach my $key (sort keys %ENV ) {
printf "%-${longest}s $s ", $key, $ENV{$key};
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.