Re: Perl calculate and average problem
- From: Josef Moellers <josef.moellers@xxxxxxxxxxxxxxxxxxx>
- Date: Fri, 30 Sep 2005 14:42:54 +0200
ed wrote:
Well here is what I have so far:
# Tell us something more ... use warnings; use strict;
@params = (1,2,3,4,5,7,8,9);
foreach $digits(@params){ $total = numbers($digits); print "$numbers\n";
Where does $numbers come from? The above pragmas would have told you it isn't defined.
}
sub numbers (){ my $digits = shift(@_);
foreach $params(@params){ $digits =+ @params;
You _do_ know that @params in scalar context is the number of elements in @params?
}
return $total }
I can't get the average of the array to even get to the other part of the code to divide the total.
Let's see if we can dig up something based upon your initial request: "calculate the average of the numbers, the total of all the numbers added together, and a new array of numbers which is the other numbers divided by 2."
my $sum = 0, $avg = 0;
my @half;
foreach (@params) {
$sum += $_;
push @half, $_ / 2;
}
$avg = $sum / @params;
#
# $avg: the average of the numbers
# $sum: the total of all the numbers added together
# @half: a new array of numbers which is the other numbers divided by 2
print "average: ", $avg, "\n";
print "total: ", $sum, "\n";
print "new array: ", join(" ", @half), "\n";
I can't, for the heck of it, find out what you mean by "It will then return a new list with all of the information, and a new array of number, the total."
-- Josef Möllers (Pinguinpfleger bei FSC) If failure had no penalty success would not be a prize -- T. Pratchett
.
- Follow-Ups:
- Re: Perl calculate and average problem
- From: axel
- Re: Perl calculate and average problem
- From: ed
- Re: Perl calculate and average problem
- References:
- Prev by Date: Re: string matching specific number of times
- Next by Date: pnotes question from newbie
- Previous by thread: Re: Perl calculate and average problem
- Next by thread: Re: Perl calculate and average problem
- Index(es):
Relevant Pages
|
|