Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

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,334
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, $_;
}
}
 

aznsj

Newbie level 6
Joined
Jan 15, 2009
Messages
11
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,334
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.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top