Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assist with Perl. Write a program that will ask the user for a given name and re

ID: 3835353 • Letter: A

Question

Assist with Perl.

Write a program that will ask the user for a given name and report the corresponding 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 following table: 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. 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 output. 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 vertically 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.

Explanation / Answer

Program-1 Code:
#!/usr/bin/perl

# Program to provide family name for a given name
# script name: family.pl

use strict;
use warnings;

# variable initialization and declaration
my $g_name;
my %family = ();

# defining family names for a given name in the hash %family
%family = (
'fred' => 'flintstone',
'barney' => 'rubble',
'wilma' => 'flintstone',
'ram' => 'Subramaniam',
'vijay' => 'Mathur',
);

# taking input from user
print " Enter a given name: ";
$g_name = <STDIN>;
chomp($g_name);

# displaying the corresponding family name of given name
print " ", $family{$g_name};
print " ";


Execution and output:
186590cb0725:Perl bonkv$ ./family.pl

Enter a given name: ram

Subramaniam
186590cb0725:Perl bonkv$ ./family.pl

Enter a given name: vijay

Mathur
186590cb0725:Perl bonkv$ ./family.pl

Enter a given name: fred

flintstone
186590cb0725:Perl bonkv$ ./family.pl

Enter a given name: barney

rubble
186590cb0725:Perl bonkv$ ./family.pl

Enter a given name: wilma

flintstone


Program-2 Code:

#!/usr/bin/perl

# program to obtain frequency of words
# script name: word_freq.pl

use strict;
use warnings;

# variable declaration and initialization
my %hword = ();

# Taking input from stdin
while (my $word = <>){
chomp($word);
$hword{$word}++;
}

# printing frequency of each word fetched from input
print " Word frequency for the given words are ";
foreach my $word (sort keys %hword){
print $word, "=>", $hword{$word}, " ";
}


Execution and output:
NOTE: Please note that you have to give one word per one line. Once you have provided all the words press ctrl+d(unix systems) to indicate end of your input so that script calculates the frequency of words.

186590cb0725:Perl bonkv$ ./word_freq.pl
fred
barney
fred
dino
wilma
fred

Word frequency for the given words are
barney=>1
dino=>1
fred=>3
wilma=>1

186590cb0725:Perl bonkv$ ./word_freq.pl
krishna
lahari
madhavi
shyam
amulya
amulya
amulya
lahari
madhavi

Word frequency for the given words are
amulya=>3
krishna=>1
lahari=>2
madhavi=>2
shyam=>1

Program-3 Code:
#!/usr/bin/perl

# program to display list of environment variables in the system
# script name: list_env.pl

use strict;
use warnings;

# variable declaration
my ($spaces, $ecol);

# printing all the environment variables available in the OS
printf " ENVIRONMENT VARIABLES AVAIABLE IN THE SYSTEM ARE ";
foreach my $env(sort keys %ENV){
# getting length of first column to vertically align the columns
$spaces = 60 - length($env);
$ecol = " " x $spaces;
printf "%-s %s %-s ", $env, $ecol, $ENV{$env};
}


Execution and output:
186590cb0725:Perl bonkv$ ./list_env.pl

ENVIRONMENT VARIABLES AVAIABLE IN THE SYSTEM ARE
Apple_PubSub_Socket_Render /private/tmp/com.apple.launchd.LixCjttvCh/Render
HOME /Users/bonkv
LANG en_US.UTF-8
LOGNAME bonkv
OLDPWD /Users/bonkv/Chegg/Python
PATH /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
PWD /Users/bonkv/Chegg/Perl
SECURITYSESSIONID 186a6
SHELL /bin/false
SHLVL 1
SSH_AUTH_SOCK /private/tmp/com.apple.launchd.BaV3jdJ71O/Listeners
TERM xterm-256color
TERM_PROGRAM Apple_Terminal
TERM_PROGRAM_VERSION 388.1
TERM_SESSION_ID A306562B-AA70-42A1-94EF-E7FE1ACFB16B
TMPDIR /var/folders/zf/z2j63j0j0xn8mhxk5y1ktp65xvw_4r/T/
USER bonkv
VERSIONER_PERL_PREFER_32_BIT no
VERSIONER_PERL_VERSION 5.18
XPC_FLAGS 0x0
XPC_SERVICE_NAME 0
_ ./list_env.pl
__CF_USER_TEXT_ENCODING 0x7BBE2898:0x0:0x0

Setting an environment environment "TESTVAR" and could see script listing the variable
186590cb0725:Perl bonkv$ TESTVAR="hello world"; export TESTVAR
186590cb0725:Perl bonkv$ ./list_env.pl

ENVIRONMENT VARIABLES AVAIABLE IN THE SYSTEM ARE
Apple_PubSub_Socket_Render /private/tmp/com.apple.launchd.LixCjttvCh/Render
HOME /Users/bonkv
LANG en_US.UTF-8
LOGNAME bonkv
OLDPWD /Users/bonkv/Chegg/Python
PATH /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
PWD /Users/bonkv/Chegg/Perl
SECURITYSESSIONID 186a6
SHELL /bin/false
SHLVL 1
SSH_AUTH_SOCK /private/tmp/com.apple.launchd.BaV3jdJ71O/Listeners
TERM xterm-256color
TERM_PROGRAM Apple_Terminal
TERM_PROGRAM_VERSION 388.1
TERM_SESSION_ID A306562B-AA70-42A1-94EF-E7FE1ACFB16B
TESTVAR hello world
TMPDIR /var/folders/zf/z2j63j0j0xn8mhxk5y1ktp65xvw_4r/T/
USER bonkv
VERSIONER_PERL_PREFER_32_BIT no
VERSIONER_PERL_VERSION 5.18
XPC_FLAGS 0x0
XPC_SERVICE_NAME 0
_ ./list_env.pl
__CF_USER_TEXT_ENCODING 0x7BBE2898:0x0:0x0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote