Perl语言入门(第四版)习题答案(2)

2019-01-07 12:00

{

if($_[$n]>$av)

{

push ( @aba,$_[$n]); } }

@aba; }

my @fred=&above_average(1..10); print\

print\

my @barney=&above_average(100,1..10); print\print\

--------------------------------/home/confish/perl/aver

5.11 练习

1、写一个程序,类似于 cat,但保持输出的顺序关系。(某些系统的名字可能是 tac 。)

如果运行此程序:./tac fred barney betty, 输出将是文件 betty 的内容,从最后一行到第一行,然后是 barney, 最后是 fred, 同样是从最后一行到第一行。(注意使用 ./ 确保调用的是你自己的程序,而非系统提供的)

----------------------------------/home/confish/perl/tac #!/usr/bin/perl -w

#a prog same as cat but reverse the string #confish@ubuntu7.10

@ARGV=reverse @ARGV; @a=reverse<>; print @a;

----------------------------------/home/confish/perl/tac

2、写一个程序,要求用户在不同的行中输入一些字符串,将此字符串打印出来,规则是:

每一条占20 个字符宽度,右对齐。为了确保正确的输出,在开头打印出一串数字作为比较(帮助调试)。注意,不要犯19 个字符宽度的错误。例如,如果输入,hello, good-bye,则输出为:

123456789012345678901234567890123456789012345678901234567890 hello good-bye

----------------------------------/home/confish/perl/20str #!/usr/bin/perl -w

#a prog that print the strings as 20 words flush right #confish@ubuntu7.10

@str=; while($i!=5) {

foreach(0..9) {

print; } $i++; } print\foreach(@str) {

printf \ }

----------------------------------/home/confish/perl/20str

3、修改上一个程序,允许用户选择宽度,如,用户输入30,hello, good-bye(在不同的行中),

则每一行的宽度为30。(提示:参阅第二章相应部分)。提示,如果选择的宽度太长,可以增加比较行的长度。

----------------------------------/home/confish/perl/20strpro #!/usr/bin/perl -w

#a prog print the strings as number usr apionted words flush right #confish@ubuntu7.10 @str=; while($i!=5) {

foreach(0..9) { print; } $i++; } print \

$num=shift @str; chomp $num;

$conv=\foreach(@str) {

printf $conv,$_; }

----------------------------------/home/confish/perl/20strpro

6.5 练习

1、写一个程序,提示用户输入 given name(名),并给出其对应的 family name(姓)。

使用你知道的人名,或者表 6- 1(如果你在计算机上花了太多时间,以致什么人都不认识):

表 6 - 1 样本数据

输入输出 fred flintstone barney rubble wilma flintstone

-------------------------------------/home/confish/perl/hash #!/usr/bin/perl -w

#hashs that print the name's family name #confish@ubuntu7.10

$family_name{\$family_name{\$family_name{\print \chomp($gn=);

print \------------------------------------/home/confish/perl/hash

2、写一个程序,读入一串单词( 一个单词一行) ◆,输出每一个单词出现的次数。(提示:

如果某个作为数字使用值是undefined 的,会自动将它转换为 0。)如果输入单词为 fred, barney, dino, wilma , fred(在不同行中),则输出的 fred 将为 ------------------------------------/home/confish/perl/counts #!/usr/bin/perl -w

#hashs that counts the appearance times of the word #print sorted

#confish@ubuntu7.10

print \foreach(<>) {

$counts{$_}++; $counts{$_}.=\ } sort %counts; print %counts;

------------------------------------/home/confish/perl/counts

7.4 练习

1、写一个程序,输出所有提到 fred 的行(不要输出其它行)。如果输入字符串 Fred, f redrick, Alfred,能匹配上吗?准备一个小的文本文件,其中包含如:“ fred lintsotne”以及类似的信息。使用这个文本文件作为此程序的输入,以及本节下面练习的输入。

-------------------------------------------/home/confish/perl/pfred #!/usr/bin/perl -w

#prog print the line which contains \#confish@ubuntu7.10 foreach(<>) {

if(/fred/) {

print $_; } }

-------------------------------------------/home/confish/perl/pfred

2、修改上面的程序,允许匹配 Fred。现在它能匹配,Fred, f redrick , Alfred 吗? (将这些

名字加入输入文件中)

-------------------------------------------/home/confish/perl/pffred #!/usr/bin/perl -w

#prog print the line which contains \#confish@ubuntu7.10 foreach(<>) {

if(/fred|Fred/) {

print $_; } }

-------------------------------------------/home/confish/perl/pffred

3、写一个程序,输出出现句号( . ) 的行,忽略其它行。使用前面练习中的文件进行练习:

它能找到 Mr. Slate 吗?

-------------------------------------------/home/confish/perl/pp #!/usr/bin/perl -w

#prog print the line which contains a point #confish@ubuntu7.10 foreach(<>) {

if(/\\./) {

print $_; } }

-------------------------------------------/home/confish/perl/pp

4、写一个程序,输出有一个字母大写,而非所有字母都大写的行。它能匹配 Fred ,而不

匹配 fred 和 FRED 吗?

-------------------------------------------/home/confish/perl/plg #!/usr/bin/perl -w

#print the line which contains not capitals only #

confish@ubuntu7.10 foreach(<>) {

if(/[a-z][A-Z]|[A-Z]+[a-z]/) {

print $_; }

}

-------------------------------------------/home/confish/perl/plg

5、额外的练习:写一个程序,它能输出所有同时提到 wilma 和 fred 的行。

-------------------------------------------/home/confish/perl/pfw #!/usr/bin/perl -w

#print a line which contains fred and wilma #confish@ubuntu7.10 foreach(<>) {

if(/wilma.+fred|fred.+wilma/) {

print $_; } }

------------------------------------------/home/confish/perl/pfw

8.10 练习

1、使用模式测试程序。创造一个模式能匹配字符串 match。使用字符串 beforematchafter 进

行测试。输出结果将其三部分放在正确位置了吗?

----------------------------------------------/home/confish/perl/mm #!/usr/bin/perl -w #match \

#confish@ubuntu7.10 while(<>) {

chomp; if(/match/) {

print \ }


Perl语言入门(第四版)习题答案(2).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:2017-2018届江苏省扬州市高三上学期期末调研测试语文试题 及答案

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: