admin 管理员组文章数量: 1184232
2024年3月9日发(作者:critical proce died)
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 $gn";
-----------------------/home/confish/perl/girth
2、修改上述程序,用户可以在程序运行时输入半径。如果,用户输入12.5,则应得到和上题一样的结果。
-----------------------/home/confish/perl/girthpro
#!/usr/bin/perl -w
#a better one to calculate girth
#confish@ubuntu7.10
print"enter the radius of the circlen";
chomp($r=
if($r>0)
{
print"the girth of the circle is
".$r*2*3.1415."n";
}
else
{
print"nonavailable!n";
}
-----------------------/home/confish/perl/girthpro
3、修改上述程序,当用户输入小于0 的数字时,程序输出的周长为0,而非负数。
-----------------------/home/confish/perl/girthzero
#!/usr/bin/perl -w
#calculate the girth and print 0 when the radius
is lower than 0
#confish@ubuntu7.10
print"enter the radius of the linen";
chomp($r=
if($r>0)
{
print"the girth of the circle is
$r*2*3.1415n";
}
else
{
print"the girth of the circle is 0n";
}
-----------------------/home/confish/perl/girthzero
1、2、3:(一起实现的)
#!/usr/bin/perl -w
$pai=3.141592654;
print "Please Input Radius:";
$r=
if ( $r lt 0 ){
print "The circumference is 0n";
}else{
$l=$r*2*$pai;
printf "The circumference is %.1fn",$l;
}
4、写一个程序,用户能输入2 个数字(不在同一行)。输出为这两个数的积。
-----------------------/home/confish/perl/product
#!/usr/bin/perl -w
#print the two number's product
#confish@ubuntu7.10
print"enter the two numbers:n";
chomp($m=
chomp($n=
print"the product of the two numbers are
".$m*$n."n";
-----------------------/home/confish/perl/product
5、写一个程序,用户能输入1 个字符串和一个数字(n)(不在同一行)。输出为,n 行这个字符串,1 次1 行(提示,使用“x”操作符)。例如,如果用户输入的是“fred”和“3”,则输出为:3 行,每一行均为fred。如果输入为“fred”和“299792”,则输出为299792 行,每一行均为fred
-----------------------/home/confish/perl/printer
#!/usr/bin/perl -w
#print a string certain times depend on the usr's
input
#confish@ubuntu7.10
print"enter a string and a number:n";
$str=
chomp($num=
print ${str}x$num;
-----------------------/home/confish/perl/printer
3.9 练习
1、写一个程序,将一些字符串(不同的行)读入一个列表中,逆向输出它。如果是从键盘输入的,那在Unix 系统中应当使用CTRL+D 表明end-of-file,在Windows 系统中使用CTRL+Z.
------------------------------------/home/confish/reprint
#!/usr/bin/perl -w
#read some input and print them in reverse
sequence
#confish@ubuntu7.10
print "enter the string please:n";
@str=reverse
print "nthe reverse strings are:n@str";
------------------------------------/home/confish/reprint
2、写一个程序,读入一串数字(一个数字一行),将和这些数字对应的人名(下面列出的)输出来。(将下面的人名列表写入代码中)。fred betty
barney dino Wilma pebbles bamm-bamm
例如,当输入为1,2,4 和2,则输出的为fred,
betty, dino, 和betty
------------------------------------/home/confish/num_to_name
#!/usr/bin/perl -w
#read some numbers and output the match
name
#confish@ubuntu7.10
$i=0;
@names=qw /fred betty barney dino Wilma
pebbles bamm-bamm/;
print"enter the numbers please:n";
chomp(@nums=
foreach(@nums)
{
@re=@names;
while($i ne $_)
{
$n=shift( @re);
$i++;
}
$i=0;
print $n,"n";
}
------------------------------------/home/confish/num_to_name
3、写一个程序,将一些字符串(在不同的行中)读入一个列表中。然后按ASCII 顺序将它们输出来。也就是说,当输入的字符串为fred, barney,
wilma, betty,则输出为barney betty fred wilma。分别在一行或不同的行将之输出。
------------------------------------/home/confish/sort_str
#!/usr/bin/perl -w
#read some strings and sort them in ASCII
#confish@ubuntu7.10
chomp(@str=sort
#@str=sort
lines
print @str,"n";
------------------------------------/home/confish/sort_str
4.11练习
1、写一个名为&total 的子程序,返回一列数字的和。
提示:子程序不应当有任何的I/O 操作;它处理调用的参数,返回处理后的值给调用者。结合下面的程序来练习,它检测此子程序是否正常工作。第一组数组之和25。
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(
print "The total of those numbers is
$user_total.n";
--------------------------------/home/confish/perl/subr
#!/usr/bin/perl -w
#a subroutine named total returns sum of
numbers
#confish@ubuntu7.10
sub total
{
foreach $n(0..$#_)
{
$sum+=$_[$n];
}
$sum;
}
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:n";
my $user_total=&total(
print"The total of those numbers is
$user_total.n";
--------------------------------/home/confish/perl/subr
2、利用上题的子程序,写一个程序计算从1 到1000 的数字的和。
--------------------------------/home/confish/perl/suber
#!/usr/bin/perl -w
#use the subroutine in last program to get the
sum of 1..1000
#confish@ubuntu7.10
sub total
{
foreach $n(0..$#_)
{
$sum+=$_[$n];
}
$sum;
}
@num=(1..1000);
$sum=&total(@num);
print"The sum of 1..1000 is $sumn";
--------------------------------/home/confish/perl/suber
3、额外的练习:写一个子程序,名为&above_average,将一列数字作为其参数,返回所有大于平均值的数字(提示:另外写一个子程序来计算平均值,总和除以数字的个数)。利用下面的程序进行测试:
my @fred = &above_average(1..10);
print "@fred is @fredn";
print "(Should be 6 7 8 9 10)n";
my @barney = &above_average(100, 1..10);
print "@barney is @barneyn";
print "(Should be just 100)n";
--------------------------------/home/confish/perl/aver
#!/usr/bin/perl -w
#to print the number which is larger than the
average
#in some numbers
#confish@ubuntu7.10
sub average
{
foreach $n(0..$#_)
{
$sum+=$_[$n];
}
$average=$sum/($#_+1);
}
sub above_average
{
@num=@_;
@aba=();
$av=&average(@num);
foreach $n(0..$#_)
{
if($_[$n]>$av)
{
push ( @aba,$_[$n]);
}
}
@aba;
}
my @fred=&above_average(1..10);
print"@fred is @fredn";
print"(Shuold be 6 7 8 9 10)n";
my @barney=&above_average(100,1..10);
print"@barney is @barneyn";
print"(Should be just 100)n";
--------------------------------/home/confish/perl/aver
5.11 练习
1、写一个程序,类似于 cat,但保持输出的顺序关系。(某些系统的名字可能是 tac 。) 如果运行此程序:./tac fred barney betty, 输出将是文件 betty 的内容,从最后一行到第一行,然后是 barney, 最后是 fred, 同样是从最后一行到第一行。(注意使用 ./ 确保调用的是你自己的程序,而非系统提供的)
----------------------------------/home/confish/perl/ta
c
#!/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,则输出为:
45678923456789
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"n";
foreach(@str)
{
printf "%21s",$_;
}
----------------------------------/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 "n";
$num=shift @str;
chomp $num;
$conv="%".++$num."s";
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{"fred"}="flintstone";
$family_name{"barney"}="rubble";
$family_name{"wilma"}="flintstone";
print "enter the given name please:n";
chomp($gn=
print "family name for $gn is
$family_name{$gn}n";
------------------------------------/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 "enter the word please:n";
foreach(<>)
{
$counts{$_}++;
$counts{$_}.="n";
}
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 "fred"
#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 "fred" or
"Fred"
#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 "match"
#confish@ubuntu7.10
while(<>)
{
chomp;
if(/match/)
{
print "Matched:|$`<$&>$'|n";
}
else
{
print "no match:|$_|n";
}
}
---------------------------------------------/home/confish/perl/mm
2、使用模式测试程序, 创造一个模式能匹配任何单词 ( w 意义下的单词), 但这个单词必需以字母 a 结尾。 它匹配 wilma而没匹配
barney 吗?它匹配 Mrs. Wilma Flintstone 吗?
wilma&fred 呢?使用前一章习题的文件进行练习(如果没有上述字符串,则加上它们)
---------------------------------------------/home/confish/perl/ma
#!/usr/bin/perl -w
#match "word" ends with a
#confish@ubuntu7.10
while(<>)
{
chomp;
if(/ab/)
{
print "Matched:|$`<$&>$'|n";
}
else
{
print "no match:|$_|n";
}
}
---------------------------------------------/home/confish/perl/ma
3、修改第二题的程序,使之将由 a 结尾的单词放到$1 之中。同时修改源代码,使此变量对应的值被放在单引号之中,如$1 contains
‘Wilma’ 。
---------------------------------------------/home/confish/perl/mapro
#!/usr/bin/perl -w
#match word end with a and storage it
#confish@ubuntu7.10
while(<>)
{
chomp;
if(/(a$)/)
{
my $temp=$1;
print "$1 contains '$`$&$''n";
}
else
{
print "no match:|$_|n";
}
}
---------------------------------------------/home/confish/perl/mapro
4、额外练习:修改第三题程序,使之能捕捉由 a
结尾的单词之后的 5 个字符(如果有那么多),并将之放入一个独立变量中。例如,如果输入的是 I saw Wilma yester day, 则紧接的 5 个字符是 yest(前有空格)。如果输入是 I , Wilma! ,则只有一个字符。它现在还能匹配 wilma 吗?
---------------------------------------------/home/confish/perl/mwa
#!/usr/bin/perl -w
#match a word end with a and print the next
five character
#confish@ubuntu7.10
while(<>)
{
if(/ab/)
{
my $temp=$';
if($temp=~/.{0,5}/)
{
my $match=$&;
print $match;
}
}
else
{
print "no match:|$_|n";
}
}
---------------------------------------------/home/confish/perl/mwa
5、写一个程序(不是测试程序), 能输出任何由空白结尾的输入行(非换行符)。 在输出的结尾处放置一个标记符,使之能标记出空白。
---------------------------------------------/home/confish/perl/ms
#!/usr/bin/perl -w
#match a space
#confish@ubuntu7.10
while(<>)
{
if(/ +$/)
{
print;
}
}
---------------------------------------------/home/confish/perl/ms
9.6 练习
1、写一个模式, 它能匹配$what 当前的内容的
3 份连续拷贝。也就是说, 如果$what 为 fred,
则此模式能匹配 fredfredfred 。如果$what 为
fred|barney ,则此模式能匹配 fredfredbarney,
barneyfredfred, barneybarneybarney , 或者其它的变种。(提示:你应当在程序的顶端设置$what 的值,如 my $what = ‘ fred|barney’ ;)
---------------------------------------------/home/confish/perl/sfb
#!/usr/bin/perl -w
#match and replace fred three times
#confish@ubuntu7.10
my $what="fred|barney";
if("fredbarneybarney"=~/($what){3}/)
{
print $`."/".$&."/".$'."n";
}
---------------------------------------------/home/confish/perl/sfb
2、写一个程序, 它可以得到当前文本文件的一个拷贝。 在拷贝的文件中, 字符串 Fred( 大小写无关) 将被 Larry 替换掉。(因此,“ Manfred Mann” 将变成 “ ManLarry
Mann” . )输入的文件名已经在命令行中指定(不需要询问用户),输出的文件名是对应的输入文件名后面加上.out。
---------------------------------------------/home/confish/perl/sfl
#!/usr/bin/perl -w
#match fred to Larry
#confish@ubuntu7.10
$^I=".out";
while(<>)
{
s/fred/Larry/i;
print;
}
---------------------------------------------/home/confish/perl/sfl
3、修改上面程序, 使之将 Fred 由 Wilma 替换, Wilma 由 Fred 替换。 如果输入的为
fred&wilma, 则输出为 Wilma$Fred。
---------------------------------------------/home/confish/perl/addc
#!/usr/bin/perl -w
#add copyright
#confish@ubuntu7.10
$^I=".out";
$a="/usr/bin/perl -w n #Copyright(C) 2008 by
Yours Truly confish";
while(<>)
{
s#/usr/bin/perls+-w#$a#i;
print;
}
---------------------------------------------/home/confish/perl/addc
4、额外练习:写一个程序在你所有的练习的答案前加上下面这样一行:
a) ## Copyright (C) 20XX by Yours Truly
将上面一行放在“ shebang” 行(Perl 程序的第一行,#!/usr/bin/perl (可能随 Perl 安装的位置而有所不同, 但是指第一行,译者注) )下面。你应当在“源文件”中修改,但请备份文件。假定你可以在命令行中同时输入程序和需要的修改的文
件名。
10.10 练习
1、写一个程序,能重复要求用户猜测某个在 1 到 100 之间的数字,直到猜对为止。你的程序应当能随机的产生一个数字,使用公式 int(1 + rand 100)。当用户猜测错误时,程序应该回应“ Too high” 或者 “ Too low ” 。如果用户输入 quit或 exit ,或者回车时,程序应立即退出。如果用户猜测正确,程序也退出。
---------------------------------------------/home/confish/perl/gn
#!/usr/bin/perl -w
#a game to guess a number
#confish@ubuntu7.10
my $y=int(1+rand(100));
print"enter a number please:n";
while(chomp($t=
{
if($t ne "exit"&&$t ne"quit" )
{
if($t>$y)
{
print"tow hign";
}
elsif($t<$y)
{
print"two lown";
}
else
{
print "an";last;
}
}
else
{
last;
}
}
--------------------------------------------/home/confish/perl/gn
11.6 练习
1、写一个程序,读入命令行中的一串文件,报告其是否可读,可写,可执行,或不存在。(提示:如果一个函数能一次对一个文件进行所有的检测将非常有帮助。) 如果一个文件被执行了
chmod 0 操作, 将报告什么?(在 Unix 系统中,chmod 0 some_file 将一个文件变成不可读,不可写,不可执行的)在大多数 shell 中,星号(*)表示当前目录中的所有的普通文件。也就是说,可以输入像 ./ex11 - 1 *这样的命令,返回当前目录下文件的属性。
------------------------------------------------/home/confish/perl/crw
#!/usr/bin/perl -w
#to check whether a file is exits have readability
and writeable
#confish@ubuntu7.10
foreach(@ARGV)
{
if(-e)
{
print "$_ exitsn";
if(-r)
{
print "$_ readablen";
}
else
{
print"$_ does not be
readablen";
}
if(-w)
{
print "$_ writeablen";
}
else
{
print "$_ does not be
writeablen";
}
}
else
{
print "$_ does not exitsn";
}
}
------------------------------------------------/home/confish/perl/crw
2、写一个程序,找出命令行中存在时间最长的文件名,并报告其天数。当参数为空时,其行为如何(例如,命令行中没有输入任何的文件)?
------------------------------------------------/home/confish/perl/et
#!/usr/bin/perl -w
#to check the longest exits file
#confish@ubuntu7.10
if(@ARGV)
{
foreach(@ARGV)
{
if(-M>$such)
{
$such=-M;
$file=$_;
}
}
print"$file has exits $such daysn";
}
else
{
print "no input filesn";
}
------------------------------------------------/home/confish/perl/et
12.14 练习
1、写一个程序要求用户输入一个目录名,再改变到那个目录去。如果用户输入的值是空白,则转变到他/她的主目录去。改变后,将这个目录下的普通内容(不包括有点(. )开头的项)按照字母顺序列出来。(提示:使用目录句柄还是
glob 更方便?)如果没有成功改变目录,提示用户,但不要尝试输出目录里的内容。
#! /usr/bin/env perl -w
use strict;
#Date:2005-05-31
#ex12_1
if (@ARGV) {
chdir $ARGV[0];
}else{
chdir "~";}
@_=glob '*';
print @_;
2、修改程序,使之包含所有的文件,不仅仅是那些不以点( . ) 开头的文件。
#! /usr/bin/env perl -w
use strict;
#Date:2005-05-31
#ex12_1
if (@ARGV) {
chdir $ARGV[0];
}else{
chdir "~";}
@_=glob '.* *';
print @_;
3、如果你在前面的练习中使用的是目录句柄,使用 glob 重写它。如果使用的是 glob ,则用目录句柄重写。
#! /usr/bin/env perl -w
use strict;
#Date:2005-05-31
#ex12_3
print "Which directory? (Default is your home
directory) ";
chomp(my $dir =
if ($dir =~ /^s*$/) { # A blank
line
chdir or die "Can't chdir to your home
directory:
$!";
} else {
chdir $dir or die "Can't chdir to '$dir':
$!";
}
opendir DOT, "." or die "Can't opendir dot:
$!";
foreach (sort readdir DOT) {
# next if /^./; ## if we were skipping
dot files
print "$_n";
}
4、写一个类似于 rm 的程序,删除命令行中出现的任何文件。(不需要处理像 rm 中的选项)
#! /usr/bin/env perl -w
use strict;
#Date:2005-05-31
#ex12_4
#类似于rm 的程序
for (@ARGV) {
unlink $_;}
5、写一个类似于 mv 的程序,将命令行中的第一个参数重命名为第二个参数。(不需要处理像
mv 中的选项,或别的参数。) 允许它是一个目录;如果是,使用相同的基本名字(basename) ,生成新的目录。
#! /usr/bin/env perl -w
use strict;
#Date:2005-05-31
#ex12_5
#类似于mv 的程序
rename $ARGV[0],$ARGV[1];
6、如果你的操作系统支持, 写一个类似于 ln 的程序, 能建立命令行中第一个参数的硬连接(hard link) 到第二个参数。(不需要处理 ln 的选项,或者更多的参数。)如果系统没有硬连接,那么输出一条消息,指出如果可行你将进行的操作。
提示:这个程序和前面的有类似的地方,注意到这一点能节约你编码的时间。
#! /usr/bin/env perl -w
use strict;
#Date:2005-05-31
#ex12_5
#类似于ln 的程序
link $ARGV[0],$ARGV[1]or warn "can't link";
7、如果你的操作系统支持,修改上一个练习的程序,使之支持 – s 选项(在别的参数前面),来表明你想创建一个软连接(soft link)而非(hard
link) 。( 甚至在你没有硬连接的情况,你也有
可能能创建软连接。)
#! /usr/bin/env perl -w
use strict;
#Date:2005-05-31
#ex12_5
#类似于ln -s的程序
symlink $ARGV[0],$ARGV[1]or warn "can't
symlink";
8、如果你的操作系统支持, 写一个程序来查找当前目录下的符号连接 (symbolic links), 并将它们的值打印出来 (如 ls– l 一样:name - >
value )。
#!/usr/bin/perl -w
use strict;
unless(@ARGV) {
die "perl $0
}
my @file=glob "$ARGV[0]/*";
foreach(@file) {
chomp $_;
my $link=readlink "$_";
if(defined $link){print "$_ -> $linkn";}
}
@a=`ls -l|grep -e ‘->’`;
13.5 练习
1、写一个程序,读入一串数字,将它们按照数字排序,将结果按右对齐的列打印出来。使用下面的数据进行检测:
17 000 04 1.50 3.14159 - 10 1.5 4 2001 90210 666
#! /usr/bin/env perl -w
use strict;
#Date:2005-05-31
#ex13_1
#读入一串数字,将它们按照数字排序,将结果按右对齐的列打印出来
@_=(17,000,04,1.50,3.14159,-10,1.5,4,2001,90210,666);
@_=sort {$a<=>$b}@_;
for (@_)
{
$_=sprintf "%8sn",$_;
print ;
}
2、写一个程序,将下例 hash 数据根据姓(last
name )按照大小写无关的字母顺序进行排序,并把结果打印出来。当last name 相同时,再按照名(firs t name) 排序(不用关心大小写)。因此,第一个输出的的名字是 Fred`s,最后一个是Betty`s。具有相同 family name 名字在一起。不要改变数据。输出名字的大小写应当和这里的一样。
my %last_name = qw{
fred flintstone Wilma Flintstone Barney Rubble
betty rubble Bamm- Bamm Rubble PEBBLES
FLINTSONE
};
#! /usr/bin/env perl -w
use strict;
#Date:2005-05-31
#ex13_2
#
my @keys = sort {
"L$last_name{$a}" cmp
"L$last_name{$b}" # by last name
or
"L$a" cmp "L$b"
# by first name
} keys %last_name;
foreach (@keys) {
print "$last_name{$_}, $_n";
# Rubble,Bamm-Bamm
}
3、写一个程序, 查找给定子串在给定字符串中出现的每一个位置, 输出子串出现的位置。 例如,给定字符串为“This is a test . ”给定子串为“is” , 它应当输出2和5。如果子串是“ a ” ,它应当输出8。如果子串为“t”呢?
#! /usr/bin/env perl -w
use strict;
#Date:2005-05-31
#ex13_3
#查找给定子串在给定字符串中出现的每一个位置,输出子串出现的位置
my $a="This is a test";
my $b="Th";
my $c=-2;
while ($c+1)
{
$c=index($a,$b,$c+1);
print "$cn";
}
14.8 练习
1、写一个程序可以转到某个特定的(写入代码中的)目录,如系统的根目录,再执行 ls – l 得到那个目录的目录列表。(如果你的是 non-
Unix 系统,使用你自己的系统命令,得到那个目录的详细列表)
#! /usr/bin/env perl -w
use strict;
#Date:2005-05-31
#ex14_1
#程序可以转到某个特定的目录,再执行ls –l
得到目录列表
chdir "~";
system "dir"
2、修改第一题的程序,将结果输出到当前目录的文件 中。错误的结果输出到文件
中。(你不需要做任何特殊的事,这两个文件中的任意一个都可能是空的。)
#! /usr/bin/env perl -w
use strict;
#Date:2005-05-31
#ex14_1
#将结果输出到当前目录的文件 中。错误的结果输出到文件 中
chdir "~";
open LOG,">>14_";
open STDERR,">>14_";
my $d= `dir`;
print LOG"$d";
3、写一个程序能解析 date 命令的输出,判断当前日期是一个星期的第几天。如果是 weekday(周一至周五),则输出get to work ;否则,输出 go play。date 命令的输出如果是由 Mon
开头,则指星期一。如果你的 non- Unix 系统
没有date 命令,则伪造一个程序使之可以输出像 date 命令那样的结果。我们这里给你提供一个两行的程序,如果你答应不问我们它工作的原因:
#! /usr/bin/env perl -w
use strict;
#Date:2005-05-31
#ex14_1
#判断当前日期是一个星期的第几天。如果是weekday(周一至周五),则输出
#get to work;否则,输出go play。
if (`date` =~ /^S/) {
print "go play!n";
} else {
print "get to work!n";
}
15.4 练习
1、从 CPAN 上安装 Module::CoreList 这个模块。打印出 Perl5.006 所附带的所有模块。创建一个 hash ,其 keys 为给定版本的 Perl 所带的模块的名字,使用下面的代码:
版权声明:本文标题:Perl语言入门(第四版)习题答案 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/b/1709970260a551667.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论