Perl语言入门(第四版)习题答案
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,而非负数。
-----------------------/home/confish/perl/girthzero #!/usr/bin/perl -w#calculate the girth and print 0 when the radius is lower than 0#confish@ubuntu7.10print"enter the radius of the line\n";chomp($r=<STDIN>);if($r>0){print"the girth of the circle is$r*2*3.1415\n";}else{print"the girth of the circle is 0\n";}-----------------------/home/confish/perl/girthzero1、2、3:(一起实现的)#!/usr/bin/perl -w$pai=3.141592654;print "Please Input Radius:";$r=<STDIN>;if ( $r lt 0 ){print "The circumference is 0\n";}else{$l=$r*2*$pai;printf "The circumference is %.1f\n",$l;}4、写一个程序,用户能输入2 个数字(不在同一行)。
汇编语言程序设计(第四版)第1~4章【课后答案】

汇编语言程序设计第四版【课后习题答案】第1章汇编语言基础知识〔习题1.1〕简述计算机系统的硬件组成及各部分作用。
〔解答〕CPU:包括运算器、控制器和寄存器组。
运算器执行所有的算术和逻辑运算;控制器负责把指指令逐条从存储器中取出,经译码分析后向机器发出各种控制命令,并正确完成程序所要求的功能;寄存器组为处理单元提供所需要的数据。
存储器:是计算机的记忆部件,它用来存放程序以及程序中所涉及的数据。
外部设备:实现人机交换和机间的通信。
〔习题1.2〕明确下列概念或符号:主存和辅存,RAM和ROM,存储器地址和I/O端口,KB、MB、GB和TB。
〔解答〕主存又称内存是主存储器的简称,主存储器存放当前正在执行的程序和使用的数据,CPU可以直接存取,它由半导体存储器芯片构成其成本高、容量小、但速度快。
辅存是辅助存储器的简称,辅存可用来长期保存大量程序和数据,CPU需要通过I/O接口访问,它由磁盘或光盘构成,其成本低、容量大,但速度慢。
RAM是随机存取存储器的英语简写,由于CPU可以从RAM读信息,也可以向RAM写入信息,所以RAM也被称为读写存储器,RAM型半导体存储器可以按地址随机读写,但这类存储器在断电后不能保存信息;而ROM中的信息只能被读出,不能被修改,ROM型半导体通常只能被读出,但这类存储器断电后能保存信息。
存储器由大量存储单元组成。
为了区别每个单元,我们将它们编号,于是,每个存储单元就有了一个存储地址,I/O接口是由一组寄存器组成,为了区别它们,各个寄存器进行了编号,形成I/O地址,通常称做I/O端口。
KB是千字节、MB是兆字节、GB是吉字节和TB是太字节,它们都是表示存储器存储单元的单位。
〔习题1.3〕什么是汇编语言源程序、汇编程序、目标程序?〔解答〕用汇编语言书写的程序就称为汇编语言源程序;完成汇编工作的程序就是汇编程序;由汇编程序编译通过的程序就是目标程序。
〔习题1.4〕汇编语言与高级语言相比有什么优缺点?〔解答〕汇编语言与高级语言相比的优点:由于汇编语言本质就是机器语言,它可以直接地、有效地控制计算机硬件,因而容易产生运行速度快,指令序列短小的高效目标程序,可以直接控制计算机硬件部件,可以编写在“时间”和“空间”两方面最有效的程序。
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.写一个程序,读入一串数字(一个数字一行),将和这些数字对应的人名(下面列出的)输出来。
【Lua程序设计第四版练习题答案】ch01Lua语言入门

【Lua程序设计第四版练习题答案】ch01Lua语⾔⼊门
联系1.1: 运⾏阶乘的⽰例并观察,如果输⼊负数,程序会出现什么问题?试着修改代码来解决问题。
-- 定义⼀个计算阶乘的函数
function fact (n)
if n == 0 then
return 1
else
return n * fact(n-1)
end
end
print("enter a number:")
a = io.read("*n") -- 读取⼀个数字
print(fact(a))
这是本章最开始提到的阶乘⽰例程序,通过实际的运⾏,我们了解到由于程序没有对负数形式进⾏校验的逻辑,因此程序会⼀直迭代递归下去,没有终⽌条件lua编译器报出堆栈溢出的错误才结束。
在进⾏修改之后的代码如下:
opefunction fact(n)
n = n or 0
if n < 0 then
error("Cannot calculate the factorial of a negative number")
elseif n == 0 then
return 1
else
return n * fact(n-1)
end
end
print("Enter a number: ")
a = io.read("*n")
print("Answer is: ", fact(a))
这⾥加⼊对输⼊负数的终⽌条件的判断,因此不会再出现堆栈溢出的错误。
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,中间产生的文件结果不保留。
汇编语言程序设计(第四版)第4章【课后答案】【精选】

汇编语言程序设计 第四版【课后习题答案】--囮裑為檤第4章 基本汇编语言程序设计〔习题4.1〕例题4.2如果要求算术右移8位,如何修改程序。
〔解答〕思路: 首先由最高位字节向次低位字节传送……次低位字节向最低位字节传送(共7次);再判最高位字节符号位,如为0,送00h 到最高位字节;如为1,送ffh 到最高位字节。
传送可参考例题4.2,不过应从第一号字节送第零号字节,……最高位字节向次低位字节传送;也可以用循环来完成: .model small .stack 256 .dataqvar dq 1234567887654321h .code .startup mov cx,7 mov si,1again: mov al, byte ptr qvar[si] mov byte ptr qvar[si-1],al inc siloop again test al,80h jz ezzmov bl,0ffh jmp done ezz: mov bl,0done: mov byte ptr qvar[7],bl .exit 0 end〔习题4.2〕例题4.2如果要求算术左移7位,如何用移位指令实现。
〔解答〕思路:可设计外循环体为8个字节左移一次,方法是:最低位字节算术左移一次, 次低位字节至最高位字节依次带 CF 位循环左移一次(内循环共8次),外循环体控制执行7次即可。
.model small .stack 256 .dataqvar dq 1234567887654321h4 11 201628.code.startupmov dx, 7 ;外循环次数mov ax, byte ptr qvar[0] ;最低位字节送axlpp: shl ax, 1 ;最低位字节左移一次,其d7移入CF 位 mov si, 1mov cx, 7 ;内循环次数again: rcl byte ptr qvar[si], 1 ;高位字节依次左移 P50 inc siloop again dec dx jnz lpp .exit 0 .end〔习题4.3〕将AX 寄存器中的16位数连续4位分成一组,共4组,然后把这4组数分别放在AL 、BL 、CL 和DL 寄存器中。
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)。
《Perl语言入门习题答案》练习1、写一个程序,计算半径为的圆的周长。
圆周长等于2π(π约为)乘以半径。
答案为。
-----------------------/home/confish/perl/girth#!/usr/bin/perl -w#this program calculate a circle's girth,$r=;$g=*2*;print "the girth of the circle is $g\n";-----------------------/home/confish/perl/girth2、修改上述程序,用户可以在程序运行时输入半径。
如果,用户输入,则应得到和上题一样的结果。
-----------------------/home/confish/perl/girthpro#!/usr/bin/perl -w`#a better one to calculate girthprint"enter the radius of the circle\n";chomp($r=<STDIN>);if($r>0){print"the girth of the circle is ".$r*2*."\n";;}else{print"nonavailable!\n";}-----------------------/home/confish/perl/girthpro3、修改上述程序,当用户输入小于0 的数字时,程序输出的周长为0,而非负数。
】-----------------------/home/confish/perl/girthzero#!/usr/bin/perl -w#calculate the girth and print 0 when the radius is lower than 0print"enter the radius of the line\n";chomp($r=<STDIN>);if($r>0)&{print"the girth of the circle is $r*2*\n";}else{print"the girth of the circle is 0\n";}-----------------------/home/confish/perl/girthzero$1、2、3:(一起实现的)#!/usr/bin/perl -w$pai=;print "Please Input Radius:";$r=<STDIN>;if ( $r lt 0 ){print "The circumference is 0\n";@}else{$l=$r*2*$pai;printf "The circumference is %.1f\n",$l;}4、写一个程序,用户能输入2 个数字(不在同一行)。
输出为这两个数的积。
-----------------------/home/confish/perl/product#!/usr/bin/perl -w、#print the two number'sprint"enter the two numbers:\n";chomp($m=<STDIN>);chomp($n=<STDIN>);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 行,每一行均为fred'-----------------------/home/confish/perl/printer#!/usr/bin/perl -w#print a string certain times depend on the usr'sprint"enter a string and a number:\n";$str=<STDIN>;chomp($num=<STDIN>);print ${str}x$num;-----------------------/home/confish/perl/printer<练习1、写一个程序,将一些字符串(不同的行)读入一个列表中,逆向输出它。
如果是从键盘输入的,那在Unix 系统中应当使用CTRL+D 表明end-of-file,在Windows 系统中使用CTRL+Z.------------------------------------/home/confish/reprint#!/usr/bin/perl -w#read some input and print them in reverseprint "enter the string please:\n";@str=reverse <STDIN>;]print "\nthe reverse strings are:\n@str";------------------------------------/home/confish/reprint2、写一个程序,读入一串数字(一个数字一行),将和这些数字对应的人名(下面列出的)输出来。
(将下面的人名列表写入代码中)。
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【$i=0;@names=qw /fred betty barney dino Wilma pebbles bamm-bamm/;print"enter the numbers please:\n";chomp(@nums=<STDIN>);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 inchomp(@str=sort<STDIN>);#@str=sort<STDIN>; will print them in diffrent linesprint @str,"\n";《------------------------------------/home/confish/sort_str练习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(<STDIN>);print "The total of those numbers is $user_total.\n";--------------------------------/home/confish/perl/subr#!/usr/bin/perl -w#a subroutine named total returns sum ofsub 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(<STDIN>);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 ofsub total{foreach $n(0..$#_){$sum+=$_[$n];;}$sum;}@num=(1..1000);$sum=&total(@num);print"The sum of 1..1000 is $sum\n";--------------------------------/home/confish/perl/suber'3、额外的练习:写一个子程序,名为&above_average,将一列数字作为其参数,返回所有大于平均值的数字(提示:另外写一个子程序来计算平均值,总和除以数字的个数)。