perl学习(三)

                      下面是我这几天学习perl写的练习。
use strict;
sub total {#求和的子例程
    my $sum = 0;
    foreach (@_) {
        $sum += $_;
    }
    return $sum;
}
sub average {#求平均的子例程
    return total(@_)/$#_;
}
sub  who_is_greater{#求比平均大的数的子例程
    my $average = average(@_);
    my @answer;
    foreach (@_) {
        if ($_ > $average) {
            push @answer,$_;
            push @answer," ";
        }
    }
    return @answer;
}
my @in = (1..10);
my @answer = who_is_greater(@in);
print @answer;
========================================================================================================
use strict;
sub max {#求最大值的子例程
    my($isMax) = shift @_;
    foreach(@_){
        if($isMax < $_){
            $isMax = $_;
        }
    }
    $isMax;
}
my @lines = qw {1 23 43 4323 32423423};
my $s = &max;
print $s,@lines;
===================================================================
use strict;
chomp(my $w = <STDIN>);
chomp(my @lines = <STDIN>);
print "1234567890" x (($w+9)/10),"\n";
my $format = "%${w}s\n" x @lines;
printf $format,@lines;
这是书上的一个例题
==========================================================================================================
use strict;
if (! open LOG,">logfile") {
    die "can't open logfile:$!";
}
print LOG "error!!!!";
close LOG;
if(! open LOG,"<logfile") {
    die "";
}
while(<LOG>) {
    chomp;
    print $_;
}
close LOG;
文件i/o操作
=================================================================================================================
chomp($x = <STDIN>); chomp($n = <STDIN>); if($n <=0) {     $n *= -1; } $i = 1; $sum = 0; $temp = $x; while($i <= $n){     $sum += $temp;     $i++;     $temp = $temp*($x/$i); } print "sum is:$sum"; 用来计算 x+x/2!+...+x/n!的值

相关文章

1. 如何去重 #!/usr/bin/perl use strict; my %hash; while(...
最近写了一个perl脚本,实现的功能是将表格中其中两列的数据...
表的数据字典格式如下:如果手动写MySQL建表语句,确认麻烦,...
巡检类工作经常会出具日报,最近在原有日报的基础上又新增了...
在实际生产环境中,常常需要从后台日志中截取报文,报文的形...
最近写的一个perl程序,通过关键词匹配统计其出现的频率,让...