課后練習
第一題
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 = <STDIN>);
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"
}
運行結(jié)果:
$ ./practice1.pl
Please input the first name:
fred #這一行是輸入的胰苏,然后回車,得到下面的結(jié)果
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 = <STDIN>); #定義數(shù)組硕并,因為我們要輸入多個名字
my %name_list; #定義hash
my $name; #定義輸入的每一個標量
foreach $name(@name){
$name_list{$name} += 1; #每輸入一個名字法焰,就往該名字對應(yīng)的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
}
運行結(jié)果:
$ ./practice1.pl
Please enter some strings, then press ctrl_D:
fred #依次輸入姓名,回車換行
barney
fred
dino
wilma
fred#輸入最后一個姓名后倔毙,回車壶栋,ctrl+D,彈出下面的結(jié)果
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; #把最長字符串的長度設(shè)置為0起始
foreach $mypath(@mypath){ #這個循環(huán)是一個查找最長字符串的操作普监,查找在key里,哪一個字符串的長度最長琉兜,然后記錄其長度
$mypath_length = length($mypath); #用length函數(shù)求長度凯正,賦值給每一個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};
} #這里要左對齊打印豌蟋,所以上面的百分號后加-號
運行結(jié)果:
$ ./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