Re: Perl calculate and average problem



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

.



Relevant Pages

  • Re: [PHP] foreach() using current() strange beahvior
    ... that's expected as foreach moves the internal array pointer, ... When using the internal pointer just by calling current(so not moving ... Unless the array is referenced, foreach operates on a copy of the ...
    (php.general)
  • Re: a question about for and foreach
    ... foreach (@array) { ... why @array are changed after this foreach loop!!! ... The "foreach" loop iterates over a normal list value and sets the ... If any element of LIST is an lvalue, you can modify it by modifying ...
    (perl.beginners)
  • Re: safe to delete elements of array in foreach
    ... I agree with Jon on this one. ... I make it a habit not to delete entries in a foreach() loop. ... build an array of keys I want to delete, and after the loop ends, delete ... I don't know whether an operation like this is guaranteed to work in PHP ...
    (comp.lang.php)
  • Re: [PHP] Foreach
    ... The problem I am having is getting both to update the table in MySQL. ... echo "$t"; ... foreach { ... // foreach expects an array, ...
    (php.general)
  • parse hash by array element seems to run slow
    ... I've been through the faq's and every web site that focuses on perl ... I iterate the array and search the hash keys to find a match and then ... foreach $verifyArr ...
    (comp.lang.perl.misc)