1、Perl 语言入门习题答案2.12 练习1、写一个程序,计算半径为12.5的圆的周长。圆周长等于2(约为3.1415926)乘以半径。答案为78.5。-/home/confish/perl/girth#!/usr/bin/perl -w#this program calculate a circles girth#confishubuntu7.10$r=12.5;$g=12.5*2*3.1415;print “the girth of the circle is $gn“;-/home/confish/perl/girth2、修改上述程序,用户可以在程序运行时输入半径。如果,用户输入12.5,
2、则应得到和上题一样的结果。 -/home/confish/perl/girthpro#!/usr/bin/perl -w#a better one to calculate girth#confishubuntu7.10print“enter the radius of the circlen“;chomp($r=);if($r0)print“the girth of the circle is “.$r*2*3.1415.“n“;elseprint“nonavailable!n“;-/home/confish/perl/girthpro3、修改上述程序,当用户输入小于0 的数字时,程序输出的
3、周长为0,而非负数。-/home/confish/perl/girthzero#!/usr/bin/perl -w#calculate the girth and print 0 when the radius is lower than 0#confishubuntu7.10print“enter the radius of the linen“;chomp($r=);if($r0)print“the girth of the circle is $r*2*3.1415n“;elseprint“the girth of the circle is 0n“;-/home/confish/per
4、l/girthzero1、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 numbers p
5、roduct#confishubuntu7.10print“enter the two numbers:n“;chomp($m=);chomp($n=);print“the product of the two numbers are “.$m*$n.“n“;-/home/confish/perl/product5、写一个程序,用户能输入1 个字符串和一个数字(n)(不在同一行)。输出为, n 行这个字符串,1 次1 行(提示,使用“x”操作符)。例如,如果用户输入的是“fred”和“3”,则输出为:3 行,每一行均为fred 。如果输入为“fred”和“299792”,则输出为299792
6、行,每一行均为fred-/home/confish/perl/printer#!/usr/bin/perl -w#print a string certain times depend on the usrs input#confishubuntu7.10print“enter a string and a number:n“;$str=;chomp($num=);print $strx$num;-/home/confish/perl/printer3.9 练习1、写一个程序,将一些字符串(不同的行)读入一个列表中,逆向输出它。如果是从键盘输入的,那在Unix 系统中应当使用CTRL+D 表明
7、end-of-file ,在Windows 系统中使用CTRL+Z. -/home/confish/reprint#!/usr/bin/perl -w#read some input and print them in reverse sequence#confishubuntu7.10print “enter the string please:n“;str=reverse ;print “nthe reverse strings are:nstr“;-/home/confish/reprint2、写一个程序,读入一串数字(一个数字一行),将和这些数字对应的人名(下面列出的)输出来。(将下面
8、的人名列表写入代码中)。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#confishubuntu7.10 $i=0;names=qw /fred betty barney dino Wilma pebbles bamm-bamm/;print“enter the numbers p
9、lease:n“;chomp(nums=);foreach(nums) re=names;while($i ne $_)$n=shift( re);$i+;$i=0; print $n,“n“;-/home/confish/num_to_name3、写一个程序,将一些字符串(在不同的行中)读入一个列表中。然后按ASCII 顺序将它们输出来。也就是说,当输入的字符串为fred, barney, wilma, betty,则输出为barney betty fred wilma。分别在一行或不同的行将之输出。-/home/confish/sort_str#!/usr/bin/perl -w#read
10、 some strings and sort them in ASCII#confishubuntu7.10chomp(str=sort);#str=sort; will print them in diffrent linesprint str,“n“;-/home/confish/sort_str4.11 练习1、写一个名为my $fred_total = print “The total of fred is $fred_total.n“;print “Enter some numbers on separate lines: “;my $user_total = print “The
11、total of those numbers is $user_total.n“; -/home/confish/perl/subr#!/usr/bin/perl -w#a subroutine named total returns sum of numbers#confishubuntu7.10sub totalforeach $n(0$#_)$sum+=$_$n;$sum;myfred=qw1 3 5 7 9;my $fred_total=print“The total of fred is $fred_total.n“;print“Enter some numbers on separ
12、ate lines:n“;my $user_total=print“The total of those numbers is $user_total.n“;-/home/confish/perl/subr2、利用上题的子程序,写一个程序计算从1 到1000 的数字的和。-/home/confish/perl/suber#!/usr/bin/perl -w#use the subroutine in last program to get the sum of 11000#confishubuntu7.10sub totalforeach $n(0$#_)$sum+=$_$n;$sum;num
13、=(11000);$sum=print“The sum of 11000 is $sumn“;-/home/confish/perl/suber3、额外的练习:写一个子程序,名为print “fred is fredn“;print “(Should be 6 7 8 9 10)n“;my barney = 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
14、average#in some numbers#confishubuntu7.10sub averageforeach $n(0$#_)$sum+=$_$n;$average=$sum/($#_+1);sub above_averagenum=_;aba=();$av=foreach $n(0$#_)if($_$n$av)push ( aba,$_$n); aba;my fred=print“fred is fredn“;print“(Shuold be 6 7 8 9 10)n“;my barney=print“barney is barneyn“;print“(Should be just
15、 100)n“;-/home/confish/perl/aver5.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#confis
16、hubuntu7.10ARGV=reverse ARGV;a=reverse)$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/
17、bin/perl -w#prog print the line which contains “fred“#confishubuntu7.10foreach()if(/fred|Fred/)print $_;-/home/confish/perl/pffred3、写一个程序,输出出现句号( . ) 的行,忽略其它行。使用前面练习中的文件进行练习:它能找到 Mr. Slate 吗?-/home/confish/perl/pp#!/usr/bin/perl -w#prog print the line which contains a point#confishubuntu7.10foreach(
18、)if(/a-zA-Z|A-Z+a-z/)print $_;-/home/confish/perl/plg5、额外的练习:写一个程序,它能输出所有同时提到 wilma 和 fred 的行。-/home/confish/perl/pfw#!/usr/bin/perl -w#print a line which contains fred and wilma#confishubuntu7.10foreach()chomp;if(/match/)print “Matched:|$|n“;elseprint “no match:|$_|n“;-/home/confish/perl/mm2、使用模式测试
19、程序, 创造一个模式能匹配任何单词 ( w 意义下的单词), 但这个单词必需以字母 a 结尾。 它匹配 wilma而没匹配 barney 吗?它匹配 Mrs. Wilma Flintstone 吗? wilmaif(/ab/)print “Matched:|$|n“;elseprint “no match:|$_|n“;-/home/confish/perl/ma3、修改第二题的程序,使之将由 a 结尾的单词放到$1 之中。同时修改源代码,使此变量对应的值被放在单引号之中,如$1 contains Wilma 。-/home/confish/perl/mapro#!/usr/bin/perl
20、-w #match word end with a and storage it#confishubuntu7.10while()if(/ab/)my $temp=$;if($temp=/.0,5/)my $match=$&;print $match;elseprint “no match:|$_|n“;-/home/confish/perl/mwa5、写一个程序(不是测试程序), 能输出任何由空白结尾的输入行(非换行符)。 在输出的结尾处放置一个标记符,使之能标记出空白。-/home/confish/perl/ms#!/usr/bin/perl -w#match a space#confis
21、hubuntu7.10while()s/fred/Larry/i;print;-/home/confish/perl/sfl3、修改上面程序, 使之将 Fred 由 Wilma 替换, Wilma 由 Fred 替换。 如果输入的为 fred$a=“/usr/bin/perl -w n #Copyright(C) 2008 by Yours Truly confish“;while( value )。#!/usr/bin/perl -wuse strict;unless(ARGV) die “perl $0 n“;my file=glob “$ARGV0/*“;foreach(file) ch
22、omp $_;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 -wuse strict;#Date:2005-05-31#ex13_1#读入一串数字,将它们按照数字排序,将结果按右对齐的列打印出来_=(17,000,04,1.50,3.1
23、4159,-10,1.5,4,2001,90210,666);_=sort $a$b_;for (_) $_=sprintf “%8sn“,$_;print ;2、写一个程序,将下例 hash 数据根据姓(last name )按照大小写无关的字母顺序进行排序,并把结果打印出来。当last name 相同时,再按照名(firs t name) 排序(不用关心大小写)。因此,第一个输出的的名字是 Freds,最后一个是 Bettys。具有相同 family name 名字在一起。不要改变数据。输出名字的大小写应当和这里的一样。my %last_name = qwfred flintstone W
24、ilma Flintstone Barney Rubblebetty rubble Bamm- Bamm Rubble PEBBLES FLINTSONE;#! /usr/bin/env perl -wuse strict;#Date:2005-05-31#ex13_2#my keys = sort “L$last_name$a“ cmp “L$last_name$b“ # by last nameor“L$a“ cmp “L$b“ # by first name keys %last_name;foreach (keys) print “$last_name$_, $_n“; # Rubbl
25、e,Bamm-Bamm3、写一个程序, 查找给定子串在给定字符串中出现的每一个位置, 输出子串出现的位置。 例如,给定字符串为“This is a test . ”给定子串为“is” , 它应当输出2和5。如果子串是“ a ” ,它应当输出8。如果子串为“t ”呢?#! /usr/bin/env perl -wuse 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);
26、print “$cn“;14.8 练习1、写一个程序可以转到某个特定的(写入代码中的)目录,如系统的根目录,再执行 ls l 得到那个目录的目录列表。(如果你的是 non- Unix 系统,使用你自己的系统命令,得到那个目录的详细列表)#! /usr/bin/env perl -wuse strict;#Date:2005-05-31#ex14_1#程序可以转到某个特定的目录,再执行ls l 得到目录列表chdir “;system “dir“2、修改第一题的程序,将结果输出到当前目录的文件 ls.out 中。错误的结果输出到文件 ls.err 中。(你不需要做任何特殊的事,这两个文件中的任意
27、一个都可能是空的。)#! /usr/bin/env perl -wuse strict;#Date:2005-05-31#ex14_1#将结果输出到当前目录的文件ls.out 中。错误的结果输出到文件 ls.err 中chdir “;open LOG,“14_2.out“;open STDERR,“14_2.err“;my $d= dir;print LOG“$d“;3、写一个程序能解析 date 命令的输出,判断当前日期是一个星期的第几天。如果是 weekday(周一至周五),则输出get to work ;否则,输出 go play。date 命令的输出如果是由 Mon 开头,则指星期一。
28、如果你的 non- Unix 系统没有date 命令,则伪造一个程序使之可以输出像 date 命令那样的结果。我们这里给你提供一个两行的程序,如果你答应不问我们它工作的原因:#! /usr/bin/env perl -wuse 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:Co
29、reList 这个模块。打印出 Perl5.006 所附带的所有模块。创建一个 hash ,其 keys 为给定版本的 Perl 所带的模块的名字,使用下面的代码:my %modules = %module:CoreList:version5.006;#! /usr/bin/env perl -w#Date:2005-05-31#ex15_1use Module:CoreList;my %modules = % $Module:CoreList:version5.006 ;print join “n“, keys %modules;2、获得当前目录文件名的列表。使用 C模块得到当前的目录,再使
30、用 C模块将目录名和文件名结合起来得到绝对路径(absolute path) 。将此路径输出在标准输出设备上,一行一条。你的解决方案应当可以移植到其它系统之中。#! /usr/bin/env perl -wuse strict;use Cwd;use File:Spec;#Date:2005-05-31#ex15_2my $d=getcwd;my $f;for (glob “*“)$f=File:Spec-catfile($d,$_);print “$fn“;3、利用前一题的输出,将其读入路径的列表中,再使用 C模块,将文件名从中分离出来。输出文件名。你的解决方案应当可以移植到其它系统之中。#
31、! /usr/bin/env perl -wuse strict;use File:Basename;#Date:2005-05-31#ex15_3for (perl ex15_2)$_=basename $_;print ;16.6 练习1、写一个程序,从文件中读入字符串,一行一个字符串,然后让用户输入模式,这个模式可能匹配上某些字符串。对于每一个模式,程序将指出文件中有多少个字符串(多少行)匹配上了,并指出是哪些。对于新的模式不需要重新读文件,将这些字符串保留在内存中。文件名可以直接写在程序之中。如果模式无效(例如,圆括号不匹配),则程序报告这个错误,并让用户继续尝试新的模式。当用户输入一
32、个空行,则程序退出。#!/usr/bin/perl -wuse strict;unless(ARGV) die “n“;open(IN,“;while(1) print “Please input a modeln“;my $model=;chomp $model;if($model=/s+$/ or ! defined $model) print “Nextn“;last;else my $cnt=0;foreach(in)if($_=/$model/) print “$_“;$cnt+;print “Total $cnt match the modeln“;next;#! /usr/bin
33、/env perl -wuse strict;#Date:2005-05-31#ex16_1my $filename = path/to/sample_text;open FILE, $filenameor die “Cant open $filename: $!“;chomp(my strings = );while (1) print “Please enter a pattern: “;chomp(my $pattern = );last if $pattern = /s*$/;my matches = eval grep /$pattern/, strings;if ($) print “Error: $“; else my $count = matches;print “There were $count matching strings:n“,map “$_n“, matches;print “n“;