#!/usr/bin/perl -wuse strict; # Perl pragma to restrict unsafe constructs
sub total{
my $sum;
foreach(@_){
$sum+=$_;
}
$sum;
}
sub average{
if(@_==0){return}
my $count=@_;
my $sum=&total(@_);
$sum/$count;
}
sub above_average{
my $average=&average(@_);
my @list;
# my $num;
# foreach $num(@_){
foreach $_(@_){
# if($num > $average){
if($_>$average){
# push @list,$num;
push @list,$_;
}
}
@list;
}
my @fred=&above_average(1..10);
print "\@fred is @fred\n";
print "(Should be 6 7 8 9 10)\n";
my @barney=&above_average(100,1..10);
print "\@barney is @barney\n";
print "(Should be just 100)\n";
阅读(430) | 评论(0) | 转发(0) |