in reply to Calculating Row Averages From a CSV File

I am rather new to array processing and perl
Everything you need to calculate row averages is in the code you just posted. Why not ask for help in understanding the script you've already got?

Your program has an error btw.

for my $i (0 .. $#col_num) {

@col_num isn't defined anywhere in the program. If you include

use strict; use warnings;

at the beginning of the program, perl will tell you about such errors.

Replies are listed 'Best First'.
Re^2: Calculating Row Averages From a CSV File
by rayv (Novice) on Aug 15, 2007 at 11:58 UTC

    FunkyMonk


    The reference to $#col_num was mistyped - the reference
    was actually $#col_count. I did use the strict and
    warnings pragmas, but as the reference was correct they
    did not come into play.


    I understand this code. What I don't understand is how
    to use the columns in each row to calculate an average
    for the row.

      Place this after shift @f.
      my $row_sum; @f = grep { $_ } @f; $row_sum += $_ for @f; print "Row average = ", $row_sum / @f, "\n";

      Given:

      AAA01,0,0,0,0,0,1 AAA02,0,0,0,0,1,2

      produces:

      Row average = 1 Row average = 1.5

      update: Fixed error pointed out here by rayv


        FunkyMonk,


        I looked at your snippet. This code will not exclude 0's from the average calculation.