can someone help me to fix the simple problem? (Perl)

Status
Not open for further replies.

aznsj

Newbie level 6
Joined
Jan 15, 2009
Messages
11
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,341
The purpose of below Perl script is to list the elements which are above the average. Unfortunately, I cannot debug it myself, can you help me ?
//////////////////////////
use warnings;
@fred =(1..10);
$sum=&total(@fred);
$ave=&ave(@fred);
@list=&ab_ave(@fred);
print "\@fred is @fred\n";
print "(should be 6 7 8 9 10) \n";

sub total
{my $sum;
foreach (@_)
{$sum +=$_;
}
$sum;
}


sub ave
{my $count=@_;
my $sum=&total(@_);
$sum/$count;
}

sub ab_ave
{my $ave=&ave(@_);
my @list;
foreach(@_)
{if ($_>$ave)
push @list, $_;
}
}
 

hi, I fix the problem.

use warnings;
@fred =(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";

$sum=&total(@fred);
$ave=&ave(@fred);
@list=&ab_ave(@fred);

sub total
{my $sum;
foreach (@_)
{$sum +=$_;
}
$sum;
}

print "the sum of \@fred is $sum \n";

sub ave
{my $count=@_;
my $sum=&total(@_);
$sum/$count;
}
print "the ave of \@fred is $ave \n";


sub ab_ave
{my $ave=&ave(@_);
my @list;
foreach(@_)
{if ($_>$ave)
{push @list, $_;}
}
@list;
}
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…