mauvis99 has asked for the wisdom of the Perl Monks concerning the following question:

Hi there,

I'm a noob to Perl and just wondering how to add all numbers in an array to give a total using a foreach loop? My code looks like this..

#! /usr/bin/perl # # # use strict; use warnings; #------------------data------------------------- my @Rainfall ; my $Index ; my ($Total, $Average) = 0, 0; #------------------program---------------------- print " Enter 12 numbers for the rainfall for each month \n"; for ( $Index = 0; $Index < 12 ; $Index++ ) { print "Enter a month's rainfall: " ; chomp($Rainfall[$Index] = <STDIN>); } # # now we have all the input, we can do things with it: # foreach (@Rainfall) { $Total = $_;
This is where im stuck! any help or another way to do this would be much appreciated!

Code tags added by Arunbear

Replies are listed 'Best First'.
Re: Adding Arrays..
by Zaxo (Archbishop) on Mar 29, 2007 at 02:56 UTC

    You'll want parens around "0,0" or you will get a surprise from precedence when you try that with non-zero values. A running total is what you're looking for:

    for (@Rainfall) ( $Total += $_ ; }

    After Compline,
    Zaxo

Re: Adding Arrays..
by dragonchild (Archbishop) on Mar 29, 2007 at 03:17 UTC
    use List::Util qw( sum ); my $Total = sum @Rainfall;

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
Re: Adding Arrays..
by NetWallah (Canon) on Mar 29, 2007 at 06:03 UTC
    Noob style and code suggestions posted here, with your code, formatted:
    #! /usr/bin/perl # # # use strict; # --- Good job on using strict and warnings !! use warnings; #------------------data------------------------- my @Rainfall ; my $Index ; # --- Perl programmers try to avoid using Indexes wh +ere not necessary. # --- IN This case, Index is NOT necessary my ($Total, $Average) = 0, 0; # Wrong intent (result would be OK) - er +ror pointed out by others #------------------program---------------------- print " Enter 12 numbers for the rainfall for each month \n"; for ( $Index = 0; $Index < 12 ; $Index++ ) # --- Perl programmers wou +ld not use "C" style loop { print "Enter a month's rainfall: " ; chomp($Rainfall[$Index]= <STDIN>); # --- } # --- Perl programmer would have done this like: # --- while (<STDIN>){ # --- chomp; # --- push @Rainfall, $_; # --- } # # now we have all the input, we can do things with it: # foreach (@Rainfall) { $Total = $_; # --- Perl programmer would have done: # --- foreach (@Rainfall) { # --- $Total += $_; # --- } # --- print "Total=$Total; Average = " , $Total/scalar(@Rainfall) , + "\n"; # Should have formatting for Avg...

         "Choose a job you like and you will never have to work a day of your life" - Confucius

      $Total += $_ for @Rainfall;

      Some perl programmers do things in different ways. Personally, I think postfix'ed flow controll is one of the sweetest bits of syntatic sugar perl provides.

      drink( $vodca + $orange ) while standing;

      @_=qw; ask f00li5h to appear and remain for a moment of pretend better than a lifetime;;s;;@_[map hex,split'',B204316D8C2A4516DE];;y/05/os/&print;
      Actually, I would use a counted loop because I don't trust users. Also, they don't have to do the counting themselves ....

      Cheers
      Chris