in reply to How to compute the avg of a column of numbers

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

Replies are listed 'Best First'.
Re: Re: How to compute the avg of a column of numbers
by pbeckingham (Parson) on Mar 31, 2004 at 20:23 UTC

    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