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

Hi Monks,

I have a column of numbers I want to push into an array @total. I would like to find the average of these. Does anyone know how to do this?

Many Thanks,
Louis
  • Comment on How to compute the avg of a column of numbers

Replies are listed 'Best First'.
Re: How to compute the avg of a column of numbers
by Limbic~Region (Chancellor) on Mar 31, 2004 at 19:57 UTC
    cosmicsoup,
    Our crystal balls are a bit foggy so we can't see your data, but here is a glimpse of possibilities.
    #!/usr/bin/perl use strict; use warnings; my $file = $ARGV[0] || 'file.txt'; open (INPUT, '<', $file) or die "Unable to open file for reading : $!" +; my @values; while ( <INPUT> ) { chomp; my @col = split /\t/; # Assume basic tab delimited push @values, $col[3]; # Column 4 is what we want } my $total = 0; $total += $_ for @values; my $average = $total / @values;
    Cheers - L~R

      Divide by zero protection?

      my $average = @values ? $total / @values : 0;

        pbeckingham,
        Sure, among other things. I left error handling up as an task for the reader.
        next if ! $col[3];
        Cheers - L~R
Re: How to compute the avg of a column of numbers
by Art_XIV (Hermit) on Mar 31, 2004 at 20:35 UTC
    use warnings; use strict; use List::Util qw(sum); my @list = (); while (<DATA>) { push @list, (split)[1]; } my $avg = (sum @list) / (scalar @list); print "Average: $avg\n"; 1; __DATA__ One 1 #First RedBaloons 99 #Second Normans 1066 #Third Thunderkiss 1965 #Fourth Warhammer 40000 #Fifth

    I'm sure there's a CPAN module that'll do it even better, though. ;)

    Hanlon's Razor - "Never attribute to malice that which can be adequately explained by stupidity"
Re: How to compute the avg of a column of numbers
by jmcnamara (Monsignor) on Mar 31, 2004 at 20:44 UTC

    Here is one way:
    my $sum = 0; $sum += $_ for @total; my $average = $sum / @total;

    And here is another:

    my $average = 0; $average = ($_ * $average + $total[$_]) / ($_ + 1) for 0 .. @to +tal -1;

    Or as a one-liner (for a space delimited file with the numbers in column 3):

    perl -lane '$a = (($. -1) * $a + $F[2])/$.; END{print $a}' file

    Or if the numbers are in the first column of the file:

    perl -lpe '$,+=$_}{$_=$,/$.' file

    --
    John.

Re: How to compute the avg of a column of numbers
by neniro (Priest) on Mar 31, 2004 at 21:55 UTC
    You can also use List::Util to get the sum, and then divide by the number of elements using scalar @array.
    #!/usr/bin/perl use strict; use warnings; use List::Util qw( sum ); my @box = ( 100, 200, 300 ); my $average = (@box) ? (sum (@box) / scalar @box) : 0 ; print $average, "\n";
    neniro

    Oh, i missed Art_XIVs node, suggesting the same.

Re: How to compute the avg of a column of numbers
by matija (Priest) on Mar 31, 2004 at 20:43 UTC
    Assuming the data is in the array,
    my $total=0; map($total+=$_,@values); my $average = @values ? $total / @values : 0;
Re: How to compute the avg of a column of numbers
by Elijah (Hermit) on Mar 31, 2004 at 23:22 UTC
    die,unless@ARGV;for(@ARGV){$n+=$_;$c++;}$t=$n/$c;print $t;