Calculating Column Averages from a CSV File
- From: RVokulich@xxxxxxxxxxxx (Country)
- Date: Thu, 09 Aug 2007 12:14:42 -0700
I am trying to calculate column averages (excluding 0's) for all the
columns in a CSV file except the first column. My input CSV file is
as follows:
pickcpua.dat
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
I have been able to calculate a column average for a particular column
(excluding 0's) using the following perl code:
#!/usr/bin/perl
use strict;
use warnings;
my $file = $ARGV[0] || 'pickcpua.dat';
my $col = $ARGV[1];
open(my $CPUFILE, "<", $file) || die "Unable to open sorted file !";
my @values;
while (<$CPUFILE>) {
chomp;
my @col = split /,/; # Assume basic comma delimited
if ($col[$ARGV[1]] > 0) {
push @values, $col[$ARGV[1]]; # Column 2 is what we want
}
}
my $total = 0;
$total += $_ for @values;
my $average = $total /@values;
print "Average CPU Time for all servers in cpuatest.dat file is
$average";
When trying to have perl calculate the column average(excluding 0's)
for all columns excluding the first column, using the following
perl code, perl is calculating the 2nd column's average 9 times rather
than computing the column average for the 2nd through 10th
column. The code is as follows:
#!/usr/bin/perl
use strict;
use warnings;
my $file = $ARGV[0] || 'pickcpua.dat';
open(my $CPUFILE, "<", $file) || die "Unable to open sorted file !";
my @values;
my @colval;
for($index=1; $index<10; $index++) {
while (<$CPUFILE>) {
chomp;
my @col = split /,/; # Assume basic comma delimited
if ($col[$index] > 0) {
push @values, $col[$index]; # Column 2 is what we want
push @colval, $col[$index]; # Column 2 is what we want
}
} ## End while loop
my $total = 0;
$total += $_ for @colval;
print "Pass $index through data \n";
print "colval is @colval \n";
print "values is @values \n";
my $average = $total /@colval;
print "Average CPU Time for Column for all servers in cpuatest.dat
file is $average \n";
} ## End of initial $index for loop
close $CPUFILE or die;
I would appreciate it if someone could tell me what I am doing wrong
in going from the calculation of one column's average to
caalculating the column average for all columns other than the first
column. I do not think I am using the for loop properly in this case
.
- Follow-Ups:
- RE: Calculating Column Averages from a CSV File
- From: Bhargav Purohit
- RE: Calculating Column Averages from a CSV File
- Prev by Date: qw with strings containing spaces
- Next by Date: Re: qw with strings containing spaces
- Previous by thread: qw with strings containing spaces
- Next by thread: RE: Calculating Column Averages from a CSV File
- Index(es):
Relevant Pages
|
Loading