博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Learning Perl chapter 4 练习题
阅读量:6939 次
发布时间:2019-06-27

本文共 1383 字,大约阅读时间需要 4 分钟。

原文发表在网易博客 2010-11-06 13:07:36

第1题和第2题

#!perl -w 

#chapter 4 , exercise one and two 
use strict; 
sub total{ 
    my $sum; 
    foreach $_ (@_){ 
        $sum += $_; 
        } 
        $sum; 
    } 
my @fred=(1,3,5,7,9); 
print "sum of (@fred) is:\t ",&total(@fred),"\n";

print "sum of 1 to 100 is :\t",&total(1..100),"\n";

第3题

#!perl -w 

#chapter 4 , exercise 3 
use strict; 
sub getSum{ 
    my $sum=0; 
    foreach $_ (@_){ 
        $sum+=$_; 
        } 
    $sum; 
    } 
sub getAverage{ 
    my @abovelist=(); 
    my $length=@_; 
    my $average=-1; 
    $average=&getSum(@_)/$length; 
    } 
sub above_average{ 
    my @abovelist; 
    my $average=&getAverage(@_); 
    foreach $_ (@_){ 
        if($_ > $average){ 
            push @abovelist, $_ 
            } 
        } 
    return @abovelist; 
    } 
my @fred=&above_average(1..10); 
print "above the average in one to ten is :(@fred)\n";

 

第4题

#!perl -w 

use strict; 
use 5.010; 
sub greet1{ 
    state $lastPerson=""; 
    if(@_ ==1 ){ 
        if($lastPerson ne ""){ 
            print "Hi,$_[0]!$lastPerson is also here.\n"; 
            }else{ 
                print "Hi,$_[0]!You are the first one here!\n"; 
                } 
        $lastPerson=$_[0]; 
        } 
    } 
&greet1("leipei"); 
&greet1("ada");

 

第5题

#!perl -w 

use strict; 
use 5.010; 
sub greet2{ 
    state @personlist; 
    if(@_ == 1){ 
        if(@personlist >0){ 
            print "Hi,$_[0]! I have seen:@personlist\n"; 
            }else{ 
                print "Hi,$_[0]! You are the firt one here.\n"; 
                } 
        push (@personlist,$_[0]); 
        } 
    } 
greet2("Fred"); 
greet2("Barney"); 
greet2("Wilma"); 
greet2("Betty");

本文转自leipei博客园博客,原文链接:http://www.cnblogs.com/leipei2352/archive/2011/05/25/2057471.html,如需转载请自行联系原作者

你可能感兴趣的文章
Web Storage与Cookie相比存在的优势:
查看>>
KVC/KVO原理详解及编程指南(转载)
查看>>
AndroidPn服务端部分bug解决方案
查看>>
LeetCode – Refresh – LRU
查看>>
201671030103 仇素龙 实验四软件工程结对项目
查看>>
自己写一个spring boot starter
查看>>
[leetcode] Subsets II
查看>>
图的邻接表表示
查看>>
linux nginx搭配https
查看>>
spring boot jpa 使用update 报错解决办法
查看>>
NIO学习系列:连网和异步IO
查看>>
struts2之action接受参数
查看>>
python set集合按行去重
查看>>
解决微信浏览器video全屏的问题
查看>>
强化学习之Q-learning ^_^
查看>>
P3373 【模板】线段树 2
查看>>
JDK动态代理
查看>>
初涉MySQL
查看>>
c# 编写windows服务
查看>>
[SequenceFile_2] SequenceFile 的基本操作
查看>>