PERL语言入门习题
Perl语言学习练习及参考答案

sub Fw_Print_Step {my ($step,$description) = @_;print ("\n\n==========================\n");print ("题$step:");if ($description) {print ("$description");}print ("\n==========================\n");}#*******************##题1:#设置变量int1的值为2my $int1=2;#设置变量int2的值为10my $int2=10;#比较变量int1与int2的大小,并打印出比较结果#*******************#Fw_Print_Step ($step++,"比较变量int1与int2的大小,并打印出比较结果"); print "\n变量int1=$int1,int2=$int2";print "\n比较结果:";if ($int1<$int2) {print "$int1 < $int2";} elsif ($int1>$int2) {print "$int1 > $int2";} else {print "$int1 = $int2";}#*******************##题2:#使用for循环打印出如下的字符。
# 1# 12# 123# 12345#*******************#Fw_Print_Step ($step++,"使用for循环打印出如下的字符。
11212312345");my $str= "";$str= $str.$_;if ($_==4) {$str= $str.$_+1;}print " $str\n";}#*******************##题3:my $str1 = "abc";my $str2 = "efg";#将上述2个字符串连接起来,并输出合并后的字符串长度#*******************#Fw_Print_Step ($step++,"将上述2个字符串\"$str1\"和\"$str2\"连接起来,并输出合并后的字符串长度");my $str =$str1.$str2;my $str_length=length($str);print "新字串$str的长度为:$str_length";#*******************##题4:#以逆序方式打印出字符串包含的各个字符,如变量为"123456789"则输出为"9","8",..."2","1". my $str1="abc123def456";#*******************#Fw_Print_Step ($step++,"以逆序方式打印出字符串包含的各个字符,如变量为\"123456789\"则输出为\"9\",\"8\",...\"2\",\"1\".");my $str=$str1;print "以逆序方式打印出字符串\"$str1\"包含的各个字符:\n";for($length=length($str1); $length>0; $length--) {$sub_str=chop($str);if ($length>1) {print "\"$sub_str\",";} else {print "\"$sub_str\".";}}#*******************##题5:#分别使用for与while循环来计算1+2+3+...+100的值#*******************#Fw_Print_Step ($step++,"分别使用for与while循环来计算1+2+3+...+100的值");print "用for循环计算1+2+3+...+100的值:\n ";my $result=0;for (1..100) {$result=$result+$_;}print "1+2+3+...+100=$result";print "\n用while循环计算1+2+3+...+100的值:\n ";my $result=0;my $num=1;while ($num<=100) {$result=$result+$num;$num++;}print "1+2+3+...+100=$result";#*******************##题6:#以逆序的方式打印出端口列表包含的成员口my @cmdArray = ("config", "int fa 0/1", "no shutdonw", "end");#*******************#Fw_Print_Step ($step++,"以逆序的方式打印出端口列表包含的成员口"); for (my $start=$#cmdArray; $start>=0; $start--) {my $array=$cmdArray[$start];print "$array\n";}#*******************##题7:#使用foreach打印出Hash表的所有下标与值my %map = ('red', 0xff0000,'green', 0x00ff00,'blue',0x0000ff);#*******************#Fw_Print_Step ($step++,"使用foreach打印出Hash表的所有下标与值"); foreach $capword (sort keys(%map)) {print ("$capword: $map{$capword}\n");}#while( ($key, $value) = Each (%Map)) {# Print "\N$key=$value;"}#*******************##题8:#使用正则匹配判断字符串是否包含error,若是打印提示信息。
试题python基础试题含答案

试题python基础试题含答案一、选择题(每题2分,共20分)1. 以下哪个是Python中的有效变量名?A. 2abcB. abc1C. abc-defD. 1abc答案:B2. 在Python中,哪个关键字用于定义类?A. functionB. classC. defD. return答案:B3. 以下哪个函数用于计算两个数的乘积?A. multiply()B. add()C. divide()D. subtract()答案:A4. 以下哪个模块用于操作文件和目录?A. osB. sysC. mathD. json答案:A5. 在Python中,哪个关键字用于定义函数?A. functionB. classC. defD. return答案:C6. 以下哪个语句用于遍历列表中的每个元素?A. for i in range(len(list)):B. for i in range(1, len(list)+1):C. for i in list:D. for i in range(list):答案:A7. 以下哪个函数用于随机生成一个整数?A. random.randint()B. random.random()C. random.randrange()D. random.choice()答案:A8. 以下哪个模块用于进行数学计算?A. mathB. osC. sysD. json答案:A9. 以下哪个关键字用于定义变量?A. intB. floatC. defD. var答案:D10. 以下哪个语句用于插入一个元素到列表的指定位置?A. list.insert(index, element)B. list.add(index, element)C. list.append(element)D. list.remove(element)答案:A二、填空题(每题2分,共20分)1. 在Python中,定义类的关键字是______。
Perl编程题-题目(1)

1.输出文件行数。
2.输出FASTA、FASTQ序列文件包含的序列条数。
3.将文件从最后一行一直输出到第一行。
4.FASTA序列反向互补。
5.FASTQ序列转FASTA序列。
6.输入文件:
输出要求:为文件中第一列(基因ID)添加描述信息。
6.根据基因ID将不同文件中该基因的某一统计信息整合输出到一个文件中。
输入文件:
11.将一个文件,按照任意给定的数字(行数),等分成几分,注意最后一份可能数据不足给出的行数。比如:一个文件10行,输入3,给出4个文件:1-3,4-6,7-9,10
输入文件:
12.求一个序列的GC含量,和N的比率。
输入文件:
输入文件:
输出要求:将两个文件中都有的基因输 Nhomakorabea,并输出其在不同文件中的log2值。
7.给出一个read,求其平均质量值。
输入文件:
8.给出两组序列,找出其中的公共序列(按名称处理)。perl
输入文件:
9.给出一条fa序列,提取其中的10-150这部分位点的序列出来(起始位点为1)。
输入文件:
10.统计fa格式的序列长度。
perl面试例题

Perl 第一阶段学习总结1 use strict 和use warnings 的作用?use strict 什么场合要用use strict?当你的程序有一定的行数时,尤其是在一页放不下时,或者是你找不到发生错误的原因时。
为什么要用 use strict?众多的原因之一是帮你寻找因为错误拼写造成的错误。
比如错误使用了'$recieve_date' 变量,但实际上你在程序中已声明的是 '$receive_date' 变量,这个错误就很难发现。
同样,use strict 迫使你把变量的范围缩到最小,使你不必担心同名变量在程序的其它部份发生不良作用。
(尽管这是 my 的功能,但是如果你使用 use strict 的话,它会强迫你用 my 声明变量,来达到上述目的)。
指定了use warnings;的话perl在编译的时候会将警告信息打印出来,否则不打印。
2 perl注释的方法?(1)‘#’用于注释单行(2) 注释一段=cut; #注释=cut 包围的代码………=cut;Or<<TXT; #注释<<TXT…TXT包围的代码………TXTOr__END__ #注释此行之后的所有代码………Or__DATA__ #可将__DATA__之后的代码作为程序的参数列表………3 单引号和双引号的区别?单引号不能内插且只能转义’\’,’’’两个字符。
双引号可以内插和转义所有字符。
4 print @array;print “@array”;print @array . “\n”有什么区别?Eg:my @array = qw/a b c/;Print @array; # abcPrint “@array” # a b c “内插时,会在数组各个元素之间自# 动添加分隔用的空格(小骆驼书原话)”Print @array . “\n” # 3 ‘.’是标量操作符,所以@array是#标量上下文。
pink练习题

pink练习题一、基础知识类1. 请列举出五种常见的编程语言及其主要用途。
2. 简述面向对象编程中的三大特性。
3. 请解释什么是数据结构,并列举出三种常见的数据结构。
4. 描述操作系统的五大功能。
5. 请说明计算机网络中的OSI七层模型。
6. 简述数据库的基本概念,包括数据库、数据库管理系统和SQL语言。
7. 请解释什么是算法,并列举出三种常见的排序算法。
8. 描述软件工程的五大过程模型。
9. 请说明计算机硬件系统的主要组成部分。
10. 简述计算机软件的分类。
二、编程实践类1. 编写一个Python程序,实现输入一个整数,输出它的阶乘。
2. 编写一个C++程序,实现输入一个字符串,输出它的反转形式。
3. 编写一个Java程序,实现一个简单的计算器功能,包括加、减、乘、除。
4. 编写一个JavaScript程序,实现一个简单的网页时钟。
5. 编写一个HTML和CSS代码,实现一个简单的网页布局。
6. 编写一个SQL查询语句,查询学生表中年龄大于18岁的学生信息。
7. 编写一个PHP程序,实现用户登录功能。
8. 编写一个React组件,实现一个待办事项列表。
9. 编写一个Node.js程序,实现一个简单的HTTP服务器。
10. 编写一个TypeScript程序,实现一个简单的类和对象。
三、算法与数据结构类1. 请用伪代码描述冒泡排序算法的实现过程。
2. 请用Python实现快速排序算法。
3. 请用C++实现链表的基本操作,包括插入、删除和查找。
4. 请用Java实现二叉树的前序遍历、中序遍历和后序遍历。
5. 请用JavaScript实现堆排序算法。
6. 请用PHP实现图的邻接矩阵表示和深度优先搜索。
7. 请用C实现哈希表的基本操作,包括插入、删除和查找。
8. 请用Go实现红黑树的插入操作。
9. 请用Rust实现跳表的数据结构。
10. 请用Swift实现并查集的数据结构。
四、操作系统与计算机网络类1. 请解释进程和线程的区别。
Perl练习题

2.12 练习写一个程序,计算半径为12.5的圆的周长。
圆周长等于2π(π约为3.1415926)乘以半径。
答案为78.5。
#!/usr/bin/perl$r=12.5;$pai=3.1415926 ;$C=2*$pai*$r;Print “$C\n”;修改上述程序,用户可以在程序运行时输入半径。
如果,用户输入12.5,则应得到和上题一样的结果。
#!/usr/bin/perl$r=<STDIN>;$pai=3.1415926 ;$C=2*$pai*$r;Print “$C\n”;修改上述程序,当用户输入小于0 的数字时,程序输出的周长为0,而非负数。
#!/usr/bin/perl$r=<STDIN>;$pai=3.1415926 ;if($r>=0){$C=2*$pai*$r;}If($r<0){$C=0;}Print “$C\n”;写一个程序,用户能输入2 个数字(不在同一行)。
输出为这两个数的积。
#!/usr/bim/perl$a=<STDIN>;$b=<STDIN>;$c=$a*$b;Print”$c”;写一个程序,用户能输入1 个字符串和一个数字(n)(不在同一行)。
输出为,n 行这个字符串,1 次1 行(提示,使用“x”操作符)。
例如,如果用户输入的是“fred”和“3”,则输出为:3 行,每一行均为fred。
如果输入为“fred”和“299792”,则输出为299792 行,每一行均为fred。
#!/usr.bin/perl$string=<STDIN>;$int=<STDIN>;$output=$string x $intprint $output;3.9练习写一个程序,将一些字符串(不同的行)读入一个列表中,逆向输出它。
如果是从键盘输入的,那在Unix 系统中应当使用CTRL+D 表明end-of-file,在Windows 系统中使用CTRL+Z.写一个程序,读入一串数字(一个数字一行),将和这些数字对应的人名(下面列出的)输出来。
perl 复习题 (答案)

PERL复习题(答案不是标准答案,仅供参考)一、选择题1. What is the simplest type of data that Perl can work with?A elementB scalarC vectorD component2. Which operator can be used to take the bottom item from an array?A popB pushC pullD plant3. Which operator is used to arrange items in character order?A ascendB sortC arrangeD descend4. Rather than using print, what is often used in Perl when formatting is important?A printfB formatC alignD show5. Which modifier can be used when matching in Perl to ignore case?A sB vC iD c6. Which operator can be used to break up a string into more than one part based upon a separator?A chopB splitC divideD parse7. What option do you use when starting Perl to tell it to run in warning mode? _-w \ use warnings (Fill in the blank.)8. Which control structure can be used to execute only a condition is false?A untilB unlessC whileD without9. Which of the following commands should be used to open a filehandle named KAREN to an existing file named sw?A open KAREN “>sw”;B open KAREN, “>sw”;C open “sw” KAREN;D open “>sw”, KAREN;10. Within Perl, which operator is used to change the working directory?A cdB chdirC pwdD wd11. Which operator can be used to access the first item in an array?A shiftB startC right$D left$12. Within a loop, which operator immediately ends execution of the loop?A nextB lastC redoD leave13. Which function can be used to make sure your program exits with a nonzero status even if there a standard error?A hashB noexitC nozeroD die14. Which of the following operators work most like simple searching/pattern matching?A /fB /gC s///D m//15. Which keyword is used in Perl to define a subroutine?A branchB subC splitD make16. You want to close the directory handle EV AN. Which of the following should you use to accomplish this?A close EV AN;B closedir EV AN;C close_directory EV AN;D none of the above17. Which of the following lines of code accomplishes the same thing as $price = $price + 9;?A $price +9;B $price =+9;C $price +=9;D 9 + $price18. What value do variables have before they are first assigned?A undefB nullC 0D nil19. Which Perl function can be used to launch a child process to run a program?A splitB spinC forkD system20. Which Perl function can be used to identify the first found occurrence of a substring?A findB indexC locateD allocate21. Which control structure can be used to loop as long as a conditional expression returns true?A untilB unlessC whileD without22. Which operator can be used to create private variables within a subroutine?A nativeB limitedC myD regional23. Which of the following operators is used to return a value in a subroutine?A returnB sendC giveD show24. Your script has just read in a variable from the keyboard and you want to strip that variable of the newline character that came in. What operator should you use to perform this operation?A tidyB trimC chompD slim25. What is the default sort order in Perl?A alphabeticB numericC ASCIID none of the above26. Within a loop, which operator jumps to the end of the loop but does not exit the loop?A nextB lastC redoD leave27. (不确定)Which of the following should be used to print values in an array that do not contain newlines and add them to the end of each entry?A print @array;B print @array\n;C print “@array\n”;D print “$@array \\”;28. Which string comparison operator would be equivalent to the numeric > operator?A neB eqC geD gt29. Which function can be thought of as the opposite of split?A linkB joinC uniteD bond30. Which operator can be used to add an item to the bottom of an array?A popB pushC pullD plant31. Which of the following mathematical operations has the highest precedence in Perl?A **B ++C +D -32. 以下哪一个字符串直接量的定义方式是错误的()(1)'thank you'(2)" "(3)"a "friend" of yours"(4)"a \"friend\" of yours"33. 以下哪一条语句是错误的()(1)$_= 'hello world';(2)$a='hello world';(3)my $b,$a='hello world';(4)my ($a,$b)=(0,'hello world');34. 要使下面的程序正常运行,while后的表达式应选()。
Perl语言入门实战习题[试题]
![Perl语言入门实战习题[试题]](https://img.taocdn.com/s3/m/01230fb9d15abe23492f4d00.png)
Perl语言入门实战习题[试题] 《Perl语言入门实战习题》一、计算FASTA文件中每条序列的长度; 输入文件,FASTA格式:注:如果输入文件在windows下产生,在Linux系统下操作时,宜先用dos2unix处理:用法:dos2unix 输入文件输出文件:Perl代码:#!/usr/bin/perl -wuse strict;unless (@ARGV==2) { # @ARGV 传给脚本的命令行参数列表die"Usage: perl $0 <input.fa> <out.len>\n"; # 当命令行参数不是2的时候输出使用说明 }my ($infile,$outfile) = @ARGV; # 把命令行参数赋值给输入文件和输出文件 open IN,$infile || die"error: can't open infile: $infile"; # 打开输入文件句柄IN open OUT,">$outfile" || die$!; # 打开输出文件句柄OUT $/=">";<IN>; # 设置输入记录分隔符为”>”,并去除第一个”>”while ( my $seq = <IN>){ # 把序列ID行和序列赋值给$seqmy $id = $1 if($seq =~ /^(\S+)/); # 获取序列IDchomp $seq; # 去掉末尾的”>”$seq =~ s/^.+?\n//; # 删除第一行$seq =~ s/\s//g; # 删除序列中的空白字符my $len = length($seq); # 计算序列长度print OUT "$id\t$len\n"; # 输出结果到输出文件}$/="\n"; # 把输入记录分隔符改为默认值close IN; # 关闭输入文件句柄close OUT; # 关闭输出文件句柄二、计算FASTA文件中每条序列的GC含量; 输入文件同上,输出文件:Perl代码:#!/usr/bin/perl -wuse strict;unless (@ARGV==2) {# @ARGV 传给脚本的命令行参数列表die"Usage: perl $0 <input.fa> <out.gc>\n";# 当命令行参数不是2的时候输出使用说明}my ($infile,$outfile) = @ARGV;# 把命令行参数赋值给输入文件和输出文件open IN,$infile || die"error: can't open infile: $infile";# 打开输入文件句柄IN open OUT,">$outfile" || die$!;# 打开输出文件句柄OUT$/=">";<IN>;# 设置输入记录分隔符为”>”,并去除第一个”>” while(<IN>){# $_=<IN>,把序列ID行和序列赋值给$_,$_= 可以省略不写my $id = $1 if(/^(\S+)/);# 获取序列IDchomp; # 去掉末尾的”>”s/^.+?\n//;# 删除第一行s/\s//g; # 删除序列中的空白字符my $GC = (tr/GC/GC/);#计算G或C碱基个数my $AT = (tr/AT/AT/);#计算A或T碱基个数my $len = $GC + $AT;# 计算序列非N长度my $gc_cont = $len ? $GC / $len : 0; #计算GC含量,如果长度为0,GC含量算0print OUT "$id\t$gc_cont\n"; # 输出结果到输出文件 }$/="\n";# 把输入记录分隔符改为默认值close IN; # 关闭输入文件句柄close OUT;# 关闭输出文件句柄三、求反相互补序列;输入文件同上,输出文件也是FASTA格式 Perl代码:#!/usr/bin/perl -wuse strict;unless (@ARGV==2) {# @ARGV 传给脚本的命令行参数列表die"Usage: perl $0 <input.fa> <out.gc>\n";# 当命令行参数不是2的时候输出使用说明}my ($infile,$outfile) = @ARGV;# 把命令行参数赋值给输入文件和输出文件open IN,$infile || die"error: can't open infile: $infile";# 打开输入文件句柄IN open OUT,">$outfile" || die$!;# 打开输出文件句柄OUT $/=">";<IN>;# 设置输入记录分隔符为”>”,并去除第一个”>” while (<IN>){# $_=<IN>,把序列ID行和序列赋值给$_,$_= 可以省略不写my $id = $1 if(/^(\S+)/);# 获取序列IDchomp; # 去掉末尾的”>”s/^.+?\n//;# 删除第一行s/\s//g; # 删除序列中的空白字符$_ = reverse $_; # 序列方向tr/ATCG/TAGC/; # 序列互补print OUT ">$id\n",$_,"\n"; # 输出结果到输出文件}$/="\n";# 把输入记录分隔符改为默认值close IN; # 关闭输入文件句柄close OUT;# 关闭输出文件句柄四、列表信息整合;输入列表1:序列长度文件输入文件2:序列测序覆盖深度文件输出文件:把上述两个列表的信息整合成一个列表,并且最后一行给出汇总结果:Perl代码#!/usr/bin/perl -wuse strict;(@ARGV==3) || die"Usage: perl $0 <list1> <list2> <combine.list>\n"; # 当命令行参数不是3的时候输出使用说明my ($infile1,$infile2,$outfile) = @ARGV;# 把命令行参数赋值给输入文件1、输入文件2和输出文件 my %id_len; # 定义一个哈希open IN1,$infile1 || die$!; # 打开第一个文件句柄while(<IN1>){my ($id,$len) = split /\s+/,$_; # split函数用空白符号切割每一行的内容$id_len{$id} = $len; # 哈希赋值:id => length}close IN1; # 关闭第一个文件句柄open IN2,$infile2 || die$!; # 打开第2个文件句柄open OUT,">$outfile" || die$!; # 打开输出文件句柄my $tol_len = 0; # 定义总长度变量,并赋值为0my $tol_depth = 0; # 定义总深度变量,并赋值为0while (<IN2>){my ($id,$depth) = split; # split函数用空白符号切割每一行的内容my $len = $id_len{$id}; # 序列长度print OUT join("\t",$id,$len,$depth),"\n"; # 输出整合信息到输出文件$tol_len += $len; # 长度累加$tol_depth += $len * $depth; # 深度累加}$tol_depth /= $tol_len; # 计算总体平均深度print OUT "Total\t$tol_len\t$tol_depth\n"; # 输出汇总结果到输出文件close IN2; # 关闭第二个输入文件句柄close OUT; # 关闭输出文件句柄五、串流程;Perl在工作中常用于串流程,现有同事写了3个perl脚本分三步将输入文件,infile.txt处理成最终的final.result:第1步:perl step1.pl infile.txt output1 第2步:perl step2.pl infile.txt output2 第3步:perl step3.pl output1 output2 final.result为提高工作效率,现需要写一个脚本使用infile.txt 作为输入文件,直接得到final.result,中间产生的文件结果不保留。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
}else{ print "unkown:$_\n"; } print "given name:\n"; }
#!/usr/bin/perl use strict; my @array = <STDIN>; print "1234567890"x4; print "\n"; foreach(@array) { printf "%20s",$_;
} #!/usr/bin/perl use strict; my @array = <STDIN>; my $long = shift @array; print "1234567890"x10; print "\n"; foreach(@array) { printf "%*s",$long+1,$_; }
sub above_average { my $sue = $sum / @_; my @result; foreach(@_) { if($_ > $average) { push @result , $_ ; } } return @result; }
sub total { my $sum; for(@_) { $sum += $_; } return $sum; }
#!/usr/bin/perl use strict; my @tmp = 1..1000; my $sum = &total(@tmp); print "total is : $sum.\n"; sub total { my $sum; for(@_)
{ $sum += $_; } return $sum; }
#!/usr/bin/perl use strict; my @fred = &above_average(1..10); print "\@fred is @fred\n"; print "(Should be 6 7 8 9 10)\n"; my @barney = &above_average(100, 1..10); print "\@barney is @barney\n"; print "(Should be just 100)\n";
1.[7] 写一个程序,类似于 cat,但保持输出的顺序关系。(某些系统的名字可能是 tac。)如果运行 此程序:./tac fred barney betty, 输出将是文件 betty 的内容,从最后一行到第一行,然后是 barney, 最后 是 fred, 同样是从最后一行到第一行。(注意使用 ./确保调用的是你自己的程序,而非系统提供的) 2.[8]写一个程序,要求用户在不同的行中输入一些字符串,将此字符串打印出来,规则是:每一条 占 20 个字符宽度,右对齐。 为了确保正确的输出,在开头打印出一串数字作为比较(帮助调试)。 注意,不要犯 19 个字符宽度的错误。例如,如果输入,hello, good-bye,则输出为: 123456789012345678901234567890123456789012345678901234567890 hello good-bye 3.[8]修改上一个程序,允许用户选择宽度,如,用户输入 30,hello, good-bye(在不同的行中),则每 一行的宽度为 30。(提示:参阅第二章相应部分)。提示,如果选择的宽度太长,可以增加比较行的 长度。 #!/usr/bin/perl use strict; my $input; while(defined($input=<>)) { printf $input; }
sub total { my $sum; for(@_) { $sum += $_;
} return $sum; }
----------------------------------------------------------------------------------
Perl 语言入门习题_第五章
Perl 语言入门习题_第四章
1.[12]写一个名为 &total 的子程序,返回一列数字的和。提示:子程序不应当有任何的 I/O 操作;它 处理调用的参数,返回处理后的值给调用者。结合下面的程序来练习,它检测此子程序是否正常工作。 第一组数组之和我 25。 2.[5]利用上题的子程序,写一个程序计算从 1 到 1000 的数字的和 3.[18]额外的练习:写一个子程序,名为 &above_average,将一列数字作为其参数,返回所有大于平 均值的数字(提示:另外写一个子程序来计算平均值,总和除以数字的个数)。 #!/usr/bin/perl use strict; my@fred = qw{ 1 3 5 7 9 }; my $fred_total = &total(@fred); print "The total of \@fred is $fred_total.\n"; print "Enter some numbers on separate lines: "; my $user_total = &total(<STDIN>); print "The total of those numbers is $user_total.\n";
#! /usr/bin/perl use strict; my %hash_name = ("fred"=> "flintstone","barney"=> "rubble","wilma"=> "flintstone",);
print "given name:\n"; while(<STDIN>){ chomp $_; if(exists $hash_name{$_}){ print "$_ => $hash_name{$_}\n"; }else{ print "unkown:$_\n"; } print "given name:\n"; }
Perl 语言入门习题_第六章
1.[7]写一个程序,提示用户输入 given name(名),并给出其对应的 family name(姓)。使用你知 道的人名,或者表 6-1(如果你在计算机上花了太多时间,以致什么人都不认识) 2.[15]写一个程序,读入一串单词(一个单词一行)◆,输出每一个单词出现的次数。(提示:如果某 个作为数字使用值是 undefined 的,会自动将它转换为 0。)如果输入单词为 fred, barney, dino, wilma, fred(在不同行中),则输出的 fred 将为 2。作为额外的练习,可以将输出的单词按照 ASCII 排序。 #! /usr/bin/perl use strict; my %hash_name = ("fred"=> "flintstone","barney"=> "rubble","wilma"=> "flintstone",);