I refactored the code quite a bit:
#!/usr/bin/env perl use strict; use warnings; my $file = $ARGV[0] || 'pickcpua.dat'; open my $CPUFILE, "<", $file or die "Unable to open sorted file $!"; my @col_tot; my @col_num; my @col_avg; while (<$CPUFILE>) { chomp; my @f = split /,/; shift @f; # remove the 1st column my $num_cols = scalar @f; for my $i (0 .. $num_cols-1) { my $val = $f[$i]; if ($val>0) { $col_num[$i]++; $col_tot[$i] += $val; } } } for my $i (0 .. $#col_num) { $col_avg[$i] = $col_tot[$i]/$col_num[$i]; print "Column ", ($i + 2), " Average = $col_avg[$i]\n"; } close $CPUFILE or die; my $total = 0; $total += $_ for @col_avg; print "Average CPU Time for Column for all servers in cpuatest.dat fil +e is "; print $total/@col_avg, "\n";
Update: Oops. After checking math (for a 2nd time!), this is what I get:
Column 2 Average = 8.27777777777778 Column 3 Average = 8.33222222222222 Column 4 Average = 8.11 Column 5 Average = 8.77 Column 6 Average = 10.4677777777778 Column 7 Average = 17.8433333333333 Column 8 Average = 6.42571428571429 Column 9 Average = 6.66571428571429 Column 10 Average = 6.36571428571429 Column 11 Average = 5.85714285714286 Column 12 Average = 6.01142857142857 Average CPU Time for Column for all servers in cpuatest.dat file is 8. +46607503607504
Is this what you are looking for?

In reply to Re: Calculating Column Averages From A CSV File by toolic
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.