The first thing I'd say is please put your array loops inside your read loop. You gain a lot of efficiency that way. The solution I came up with was (and I'm probably abusing some variables):

#!/bin/perl use warnings; use strict; package main; my @averages = (); my $count = 0; while (my $data = <DATA>) { my @cols = split /\,/, $data; $count++; if ( $count == 1 ) { @averages = @cols; } else { for( my $i = 1; $i < scalar @averages; $i++ ) { $averages[$i] += $cols[$i]; } } } for( my $i = 1; $i < scalar @averages; $i++ ) { print $averages[$i]/$count, "\n"; } __DATA__ IMAGINGNY,1.45,0.42,1.54,1.49,1.47,1.36,1.81,0.47,1.8,0.55,0.38 JBSQLTST,1.29,1.09,1.13,1.88,1.11,1.44,1.25,1.23,1.05,1.39,1.61 SNYCSQL,4.58,4.24,3.87,3.9,4.13,2.04,3.34,7.6,3.58,1.26,7.45 Snynetsrv,26,26.34,24.59,26.46,26.24,26.14,32.35,31.77,31.77,29.92,26. +59 W32SPLMCOR01,8.27,13.23,7.73,8.85,9.15,13.95,0,0,0,0,0 W32SDAPSCT01,3.07,3.14,2.97,3.28,21.65,54.23,3.16,3.02,3.26,2.77,3.40 W32SDASALM01,1.22,1.3,1.11,0.92,1.57,1.06,1.01,0.87,0.93,2.26,0.91 W32SMSCSD02,15.38,12,22.32,23.3,19.74,46.42,2.06,1.7,2.17,2.85,1.74 W32SPLMCOR02,13.24,13.23,7.73,8.85,9.15,13.95,0,0,0,0,0
And the output is:
C:\Code>perl columns.pl 8.27777777777778 8.33222222222222 8.11 8.77 10.4677777777778 17.8433333333333 4.99777777777778 5.18444444444444 4.95111111111111 4.55555555555556 4.67555555555556
If you want to deal with irregular length CSV, you'll need some logic to handle the different counts per column. Something like a push of a $value, $count will be necessary.

In reply to Re: Calculating Column Averages From A CSV File by dwm042
in thread Calculating Column Averages From A CSV File by country1

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.