课后练习
第一题
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:
脚本:
#!/usr/bin/perl
use warnings;
use strict;
use v5.24;
my %name_list = qw{
fred flintstone
barney rubble
wilma flintsone
};
print "Please input the first name:\n";
chomp(my $name = );
if (exists $name_list{$name}) {
print "The last name of $name is: $name_list{$name}.\n ";
}else{
print "Sorry, this person is not exist in this list.\n"
}
运行结果:
$ ./practice1.pl
Please input the first name:
fred #这一行是输入的,然后回车,得到下面的结果
The last name of fred is: flintstone.
$ ./practice1.pl
Please input the first name:
Yan #如果输入一个hash里没有的名字,则显示列表里没有这个人
Sorry, this person is not exist in this list.
第二题
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 output.
脚本:
#!/usr/bin/perl
use warnings;
use strict;
use v5.24;
print "Please enter some strings, then press ctrl_D:\n";
chomp(my @name = ); #定义数组,因为我们要输入多个名字
my %name_list; #定义hash
my $name; #定义输入的每一个标量
foreach $name(@name){
$name_list{$name} += 1; #每输入一个名字,就往该名字对应的value值上加1
}
print "how many times each word was seen:\n";
foreach $name(sort keys %name_list) { #按照key的顺序排列hash
print "$name $name_list{$name}\n"; #分别打印key和value
}
运行结果:
$ ./practice1.pl
Please enter some strings, then press ctrl_D:
fred #依次输入姓名,回车换行
barney
fred
dino
wilma
fred#输入最后一个姓名后,回车,ctrl+D,弹出下面的结果
how many times each word was seen:
barney 1
dino 1
fred 3
wilma 1
第三题
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.
#!/usr/bin/perl
use warnings;
use strict;
use v5.24;
my @mypath = keys %ENV;
my $mypath;
my $mypath_length;
my $longest = 0; #把最长字符串的长度设置为0起始
foreach $mypath(@mypath){ #这个循环是一个查找最长字符串的操作,查找在key里,哪一个字符串的长度最长,然后记录其长度
$mypath_length = length($mypath); #用length函数求长度,赋值给每一个mypath_length
if ($mypath_length > $longest){ #如果该值比longest长,则把当前的mypath_length赋值给longest
$longest = $mypath_length;
}
}
foreach $mypath(sort keys %ENV){
printf "%-${longest}s %s\n",$mypath,$ENV{$mypath};
} #这里要左对齐打印,所以上面的百分号后加-号
运行结果:
$ ./practice1.pl
CONDA_DEFAULT_ENV base
CONDA_EXE /home/yanfang/anaconda3/bin/conda
CONDA_PREFIX /home/yanfang/anaconda3
CONDA_PROMPT_MODIFIER (base)
CONDA_PYTHON_EXE /home/yanfang/anaconda3/bin/python
CONDA_SHLVL 1
HOME /home/yanfang
HOSTTYPE x86_64
LANG C.UTF-8
LD_LIBRARY_PATH ./GISTIC/MATLAB_Compiler_Runtime/v83/runtime/glnxa64:./GISTIC/MATLAB_Compiler_Runtime/v83/bin/glnxa64:./GISTIC/MATLAB_Compiler_Runtime/v83/sys/os/glnxa64:args2/MATLAB_Compiler_Runtime/v83/runtime/glnxa64:args2/MATLAB_Compiler_Runtime/v83/bin/glnxa64:args2/MATLAB_Compiler_Runtime/v83/sys/os/glnxa64:
LESSCLOSE /usr/bin/lesspipe %s %s
LESSOPEN | /usr/bin/lesspipe %s
LOGNAME yanfang
LS_COLORS rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;
#!/usr/bin/perl
43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.zst=01;31:*.tzst=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.wim=01;31:*.swm=01;31:*.dwm=01;31:*.esd=01;31:*.jpg=01;35:*.jpeg=01;35:*.mjpg=01;35:*.mjpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36:
MOTD_SHOWN update-motd
NAME LAPTOP-7T35S8KB
OLDPWD /home/yanfang
PATH /home/yanfang/anaconda3/bin:/home/yanfang/anaconda3/condabin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/mnt/c/Program Files (x86)/Common Files/Oracle/Java/javapath:/mnt/c/Rtools/bin:/mnt/c/Windows/system32:/mnt/c/Windows:/mnt/c/Windows/System32/Wbem:/mnt/c/Windows/System32/WindowsPowerShell/v1.0:/mnt/c/Program Files (x86)/NVIDIA Corporation/PhysX/Common:/mnt/c/Program Files (x86)/Skype/Phone:/mnt/c/WINDOWS/system32:/mnt/c/WINDOWS:/mnt/c/WINDOWS/System32/Wbem:/mnt/c/WINDOWS/System32/WindowsPowerShell/v1.0:/mnt/c/WINDOWS/System32/OpenSSH:/mnt/d/PuTTY:/mnt/d/VScode/Microsoft VS Code/bin:/mnt/c/Users/Yan Fang/scoop/shims:/mnt/c/Users/Yan Fang/AppData/Local/Programs/Python/Python38/Scripts:/mnt/c/Users/Yan Fang/AppData/Local/Programs/Python/Python38:/mnt/c/Users/Yan Fang/AppData/Local/Microsoft/WindowsApps:/mnt/d/python/PyCharm Community Edition 2019.3.4/bin:/snap/bin
PWD /home/yanfang/perl_practice
SHELL /bin/bash
SHLVL 1
TERM xterm-256color
USER yanfang
WSLENV WT_SESSION::WT_PROFILE_ID
WSL_DISTRO_NAME Ubuntu-20.04
WT_PROFILE_ID {07b52e3e-de2c-5db4-bd2d-ba144ed6c273}
WT_SESSION 59696fa0-02bf-4aad-973f-7c24b2174e9b
XAPPLRESDIR ./GISTIC/MATLAB_Compiler_Runtime/v83/X11/app-defaults:args2/MATLAB_Compiler_Runtime/v83/X11/app-defaults:
XDG_DATA_DIRS /usr/local/share:/usr/share:/var/lib/snapd/desktop
_ ./practice1.pl
_CE_CONDA
_CE_M