perl解题
perl语言期末考核大作业

Perl程序设计课程考核院系部门:学生专业:学生学号:学生姓名:2012年1月课程大作业内容第一题:请问Bioperl怎样安装和配置,请结合文字图表概述Bioperl 的安装、配置与测试,并简述该过程中出现问题解决经历(10分)。
一、安装过程:Bioperl的安装(1):安装好active perl 。
(2)、打开ppm,得到如下界面:3:,选择Edit >> Preferences。
得到如下界面:4:在Preferences添加下面的站点Repositories to addName perl perlBioPerl-RegularDIST DIST ReleasesBioPerl-Rel DIST/RC DIST/RCeaseCandidatesKobes:选择View >> All Packages;6:在搜索框输入bioperl得到如下界面:7:,选择最新版本,右键安装(需要安装:bioperl;bioperl-db;bioperl-network;bioperl-run)。
8:查看安装是否成功:查看目录Perl\lib\Bio中是否存在文件,存在则表明安装成功二:测试测试代码:建立一个PL文件:写入以下内容,然后双击文件,会在同目录下生成一个文件use Bio::Seq;use Bio::SeqIO;# create a sequence object of some DNAmy $seq = Bio::Seq->new(-id => 'testseq', -seq => 'CATGTAGATAG');# print out some details about itprint "seq is ", $seq->length, " bases long\n";print "revcom seq is ", $seq->revcom->seq, "\n";# write it to a file in Fasta formatmy $out = Bio::SeqIO->new(-file => '>', -format => 'Fasta'); $out->write_seq($seq);双击,得到或者在命令处理程序中:输入:C:\Perl>perldoc Bio::Biblio 得到如下:三、简述该过程中出现问题解决经历第二题:结合图形和程序回答下面问题1、编程实现一个DNA序列文件的酶切位点的分析(包括酶切位点统计、酶切位点的标记和计数,以及可视化输出等,不准用模式匹配知识)(10分)。
使用Perl编程技术解决日志分析问题

使用Perl编程技术解决日志分析问题日志分析是一项重要的任务,它可以帮助我们了解系统的运行情况、用户行为以及潜在的问题。
在大规模的系统中,日志文件往往非常庞大且杂乱,手动分析几乎是不可能的。
因此,我们需要借助编程技术来解决这个问题。
Perl作为一种强大的脚本语言,具有强大的文本处理能力,非常适合用于日志分析。
首先,我们可以使用Perl编写一个脚本来读取和处理日志文件。
Perl提供了丰富的文件处理函数和正则表达式,可以轻松地从日志文件中提取所需的信息。
例如,我们可以使用Perl的open函数打开一个日志文件,并使用while循环逐行读取文件内容。
然后,我们可以使用正则表达式匹配关键字,如IP地址、URL、错误信息等,从而提取出我们感兴趣的信息。
其次,Perl还提供了强大的字符串处理函数,可以方便地对提取出的信息进行进一步的处理和分析。
例如,我们可以使用Perl的split函数将一行日志分割成不同的字段,然后对这些字段进行统计和计算。
我们可以统计每个IP地址的访问次数,分析用户的访问行为,或者计算特定错误出现的频率等等。
Perl的字符串处理函数非常灵活,可以满足各种不同的分析需求。
此外,Perl还支持各种数据结构和模块,可以帮助我们更好地组织和处理日志数据。
例如,我们可以使用Perl的哈希表来存储IP地址和访问次数的对应关系,以便快速地进行查询和统计。
我们还可以使用Perl的数组来存储特定错误出现的时间戳,以便分析错误出现的趋势和规律。
此外,Perl还有许多强大的模块,如DateTime、Statistics::Descriptive等,可以帮助我们进行更复杂的分析和计算。
除了基本的日志分析功能,Perl还可以与其他工具和系统进行集成,进一步提高分析的效率和准确性。
例如,我们可以使用Perl的系统调用功能调用外部命令,如grep、awk等,来对日志文件进行过滤和处理。
我们还可以使用Perl的网络编程功能,将分析结果发送到远程服务器或者写入到数据库中。
perl解json数组

perl解json数组<pre name="code" class="cpp">http://11.36.10.82:4000/api/bus?bus=307&flag=1返回json是: 其实就是数组的引⽤编码成json["","黄龙体育中⼼","教⼯路花园亭","花园新村","浙江⼯商⼤学","保亭巷","教⼯路塘河路⼝","⼤关桥西","长乐路","⼆纺机总⼚","沈塘湾","善贤社区","善贤坝","皋亭坝","李王桥","袁家村","张家园","杭玻","焦化分⼚","沈家桥","⽑⽵⼭","薄板分⼚","刘⽂村jrhmpt01:/root# cat y1.pluse LWP::UserAgent;use Data::Dumper;use JSON qw/encode_json decode_json/;my $ua = LWP::UserAgent->new;$phone=$ARGV[0];$cc=$ARGV[1];$ua->timeout(10);$ua->env_proxy;$ua->agent("Mozilla/8.0");$host = "http://115.236.160.82:3000/api/bus?bus=307&flag=1";my $response = $ua->get($host);$ua->default_headers;if ($response->is_success) {print $response->decoded_content; # or whatevermy $json_out=$response->decoded_content;$arr = decode_json($json_out);}else {die $response->status_line;};print "11111111111111\n";use Data::Dumper;my $xx= Dumper($arr);print $xx;print "\n";print "111111*********\n";print $arr->[1]."\n"jrhmpt01:/root# perl y1.pl["","黄龙体育中⼼","教⼯路花园亭","花园新村","浙江⼯商⼤学","保亭巷","教⼯路塘河路⼝","⼤关桥西","长乐路","⼆纺机总⼚","沈塘湾","善贤社区","善贤坝","皋亭坝","李王桥","袁家村","张家园","杭玻","焦化分⼚","沈家桥","⽑⽵⼭","薄板分⼚","刘⽂村$VAR1 = ['',"\x{9ec4}\x{9f99}\x{4f53}\x{80b2}\x{4e2d}\x{5fc3}","\x{6559}\x{5de5}\x{8def}\x{82b1}\x{56ed}\x{4ead}","\x{82b1}\x{56ed}\x{65b0}\x{6751}","\x{6d59}\x{6c5f}\x{5de5}\x{5546}\x{5927}\x{5b66}","\x{4fdd}\x{4ead}\x{5df7}","\x{6559}\x{5de5}\x{8def}\x{5858}\x{6cb3}\x{8def}\x{53e3}","\x{5927}\x{5173}\x{6865}\x{897f}","\x{957f}\x{4e50}\x{8def}","\x{4e8c}\x{7eba}\x{673a}\x{603b}\x{5382}","\x{6c88}\x{5858}\x{6e7e}","\x{5584}\x{8d24}\x{793e}\x{533a}","\x{5584}\x{8d24}\x{575d}","\x{768b}\x{4ead}\x{575d}","\x{674e}\x{738b}\x{6865}","\x{8881}\x{5bb6}\x{6751}","\x{5f20}\x{5bb6}\x{56ed}","\x{676d}\x{73bb}","\x{7126}\x{5316}\x{5206}\x{5382}","\x{6c88}\x{5bb6}\x{6865}","\x{6bdb}\x{7af9}\x{5c71}","\x{8584}\x{677f}\x{5206}\x{5382}","\x{5218}\x{6587}\x{6751}"];111111*********Wide character in print at y1.pl line 31.黄龙体育中⼼。
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 正则表达式例题当然可以!以下是一些使用 Perl 正则表达式的例题:1. 匹配数字:```perl$string = "我有100个苹果和20个香蕉";if ($string =~ /(\d+)/) {print "匹配到的数字是: $1\n";}```输出:```匹配到的数字是: 100```2. 匹配邮箱地址:```perl$email = "";if ($email =~ /([a-zA-Z0-9_-]+[a-zA-Z0-9_-]+\.[a-zA-Z]{2,})/) { print "匹配到的邮箱地址是: $1\n";}```输出:```匹配到的邮箱地址是:```3. 匹配日期格式:YYYY-MM-DD```perl$date = "今天是";if ($date =~ /(\d{4})-(\d{2})-(\d{2})/) {print "匹配到的年份是: $1, 月份是: $2, 日期是: $3\n";}输出:```yaml匹配到的年份是: 2023, 月份是: 07, 日期是: 19```4. 替换字符串中的内容:```perl$text = "我喜欢吃苹果和香蕉";$new_text = $text =~ s/苹果/桃子/;print $new_text; 输出: 我喜欢吃桃子和香蕉```这些例题可以帮助你理解 Perl 正则表达式的使用方法和功能。
当然,Perl 正则表达式还有更多高级的用法和技巧,你可以查阅相关资料或参考 Perl 的官方文档来深入学习。
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,中间产生的文件结果不保留。
Perl语言入门(第四版)习题标准答案

Perl语言入门(第四版)习题答案————————————————————————————————作者:————————————————————————————————日期:《Perl语言入门习题答案》2.12 练习1、写一个程序,计算半径为12.5的圆的周长。
圆周长等于2π(π约为3.1415926)乘以半径。
答案为78.5。
-----------------------/home/confish/perl/girth#!/usr/bin/perl -w#this program calculate a circle's girth#confish@ubuntu7.10$r=12.5;$g=12.5*2*3.1415;print "the girth of the circle is $g\n";-----------------------/home/confish/perl/girth2、修改上述程序,用户可以在程序运行时输入半径。
如果,用户输入12.5,则应得到和上题一样的结果。
-----------------------/home/confish/perl/girthpro#!/usr/bin/perl -w#a better one to calculate girth#confish@ubuntu7.10print"enter the radius of the circle\n";chomp($r=<STDIN>);if($r>0){print"the girth of the circle is ".$r*2*3.1415."\n";}else{print"nonavailable!\n";}-----------------------/home/confish/perl/girthpro3、修改上述程序,当用户输入小于0 的数字时,程序输出的周长为0,而非负数。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
1.单元1 输入"Hello,world"这个程序,让它运行起来。
(你可以任意命名,但像ex1_1这样的名字就显得好些,它表示第一章第一个练习。
)ex1_1:#! /usr/bin/perlprint "Hello,world\n";2 在命令行输入perldoc -u -f atan2这个命令,注意它的输出。
=over 8=item atan2 Y,XX<atan2> X<arctangent> X<tan> X<tangent>Returns the arctangent of Y/X in the range -PI to PI.For the tangent operation, you may use the C<Math::Trig::tan>function, or use the familiar relation:sub tan { sin($_[0]) / cos($_[0]) }Note that atan2(0, 0) is not well-defined.3 运行第二个例子,观察它的输出。
(提示:注意正确输入这些标点符号)。
注意到和第二题输出的不同地方了吗?ex1_3:#! /usr/bin/perl@lines=`perldoc -u -f atan2`;foreach(@lines){s/\w<({^>] )/\U$l/g;print;}=over 8=item atan2 Y,XX<atan2> X<arctangent> X<tan> X<tangent>Returns the arctangent of Y/X in the range -PI to PI.For the tangent operation, you may use the C<Math::Trig::tan>function, or use the familiar relation:sub tan { sin($_[0]) / cos($_[0]) }Note that atan2(0, 0) is not well-defined.2.单元1 写一个程序,计算半径为12.5的圆的周长。
圆周长等于2л(л约为3.141592654)乘以半径。
答案是78.5。
ex2_1:#! /usr/bin/perl$c=2*3.141592654*12.5;print $c."\n";2 修改上述程序,用户可以在程序运行时输入半径。
如果,用户输入12.5,则应得到和上题一样的结果。
ex2_2:#! /usr/bin/perlchomp($r=<STDIN>);print $c=2*3.141592654*$r."\n";3 修改上述程序,当用户输入小于0的数字时,程序输出的周长为0,而非负数ex2_3:#! /usr/bin/perlchomp($r=<STDIN>);if ($r<0){$c=0;}else{$c=2*3.141592654*$r;}print $c."\n";4 写一个程序,用户能输入2个数字(不在同一行)。
输出为这两个数的积ex2_4:#! /usr/bin/perlchomp($n1=<STDIN>);chomp($n2=<STDIN>);print $n1*$n2."\n";5 写一个程序,用户能输入1个字符串和一个数字(n)(不在同一行)。
输出为,n行这个字符串,1次1行(提示,使用"x"操作符)。
例如,如果用户输入的是"fred"和"3",则输出为:3行,每一行均为fred。
如果输入为"fred"和"299792",则输出为 299792行,每一行均为fred。
ex2_5:#! /usr/bin/perl$string=<STDIN>;$n=<STDIN>;print $string x $n;3.单元1 写一个程序,将一些字符串(不同的行)读入一个列表中,逆向输出它。
如果是从键盘输入的,那在Unix系统中应当使用 CTRL D标明end-of-file,在Windows系统中使用CTRL Z. ex3_1:#! /usr/bin/perl@strings=<STDIN>;print reverse @strings;2 写一个程序,读入一串数字(一个数字一行),将和这些数字对应的人名输出来。
将下面的人名列表写入代码中)。
例如,当输入为1,2,4和2,则输出的为fred,betty,dino,和betty:fred betty barney dino Wilma pebbles bamm-bammex3_2:#! /usr/bin/perl@name=qw/fred betty barney dino Wilma pebbles bamm-bamm/;@n=<STDIN>;foreach(@n){print $name[$_-1]."\n";}3 写一个程序,将一些字符串(在不同的行中)读入一个列表中。
然后按ASCII顺序将它们输出来。
也就是说,当输入的字符串为fred,barney,wilma,betty,则输出为barney betty fredwilma。
分别在一行或不同的行将之输出。
ex3_3:#! /usr/bin/perl@strings=<STDIN>;chomp(@cstring=@string);@cstring1=sort @cstring;print sort @strings;print "@cstring1";4.单元1 写一个名为&total的子程序,返回一列数字的和。
提示:子程序不应当有任何的I/O操作;它处理调用的参数,返回处理后的值给调用者。
结合下面的程序来练习,它检测此子程序是否正常工作。
第一组数组之和为25。
ex4_1:#! /usr/bin/perlmy @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";sub total{$total=0;foreach(@_){$total =$_;}return $total;}2 利用上题的子程序,写一个程序计算从1到1000的数字的和ex4_2:#! /usr/bin/perl@numbers=1..1000;$numbers_total=&total(@numbers);print "The total of numbers from 1 to 1000 is $numbers_total.\n";sub total{$total=0;foreach(@_){$total =$_;}return $total;}3 额外的练习:写一个子程序,名为&above_average,将一列数字作为其参数,返回所有大于平均值的数字(提示:另外写一个子程序来计算平均值,总和除以数字的个数)。
利用下面的程序进行测试:ex4_3:#! /usr/bin/perlmy @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";sub above_average{undef(@numbers); #把@numbers的内容清空$average=&average(@_);foreach(@_){if($_>$average){push @numbers,$_;}}return @numbers;}sub average{$n=0;$total=0;foreach(@_){$total =$_;$n ;}$average=$total/$n;}5.单元1 写一个程序,类似于cat,但保持输出的顺序关系。
(某些系统的名字可能是tac。
)如果运行此程序:./tacfred barneybetty,输出将是文件betty的内容,从最后一行到第一行,然后是barney,最后是fred,同样是从最后一行到第一行。
(注意使用./确保调用的是你自己的程序,而非系统提供的)ex5_1:#! /usr/bin/perlprint reverse <>;2 写一个程序,要求用户在不同的行中输入一些字符串,将此字符串打印出来,规则是:每一条占20个字符宽度,右对齐。
为了确保正确的输出,在开头打印出一串数字作为比较(帮助调试)。
注意,不要犯19个字符宽度的错误。
例如,如果输入,hello,good-bye,则输出为:123456789012345678901234567890hellogood-byeex5_2:#! /usr/bin/perlchomp(@strings=<STDIN>);print "123456789012345678901234567890\n";foreach(@strings){printf " s\n",$_;}3 修改上一个程序,运行用户选择宽度,如,用户输入30,hello,good-bye(在不同的行中),则每一行的宽度为30。
ex5_3:#! /usr/bin/perlchomp($n=<STDIN>);chomp(@strings=<STDIN>);print "123456789012345678901234567890\n";foreach(@strings){printf "%${n}s\n",$_;}6.单元1 写一个程序,提示用户输入given name(名),并给出其对应的family name(姓)。