[已解决]比较大小并调用子程序的问题

[已解决]比较大小并调用子程序的问题

写一个子程序,名为&above_average,将一列数字作为其参数,返回所有大于平均值的数字

#! /usr/bin/perl -w
my @fred1=&average(1..10);
print "\@fred is @fred1\n";
my @fred=&above_average(1..10);

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";

sub average{
my($sum)=shift@_;
foreach(@_){
$sum=$sum+$_;
}
my($average)=$sum/10;
$average;
}

那个比较调用的子程序想了半天想不出来了,所以来向大家请教

比较就用 >号不行么
呵呵,解决啦

#! /usr/bin/perl -w
my @fred1=&average(1..10);
print "\@fred is @fred1\n";
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";

sub average{
my($sum)=shift@_;
foreach(@_){
$sum=$sum+$_;
}
my($average)=$sum/10;
$average;
}

sub above_average{
my $code= &average(@_);
my @result;
foreach( @_ ){
  if( $_ >$code ){
   push( @result, $_ );
  }
}
@result;
}