Great Monks,
I've progressed with my quest for a script to calculate the averages of all columns. However, things are not working like I hoped. The final loop in my script "should" create a SELECT statement to calculate the average of the column (except the first one, but I'll try that later). It isn;t working though.
My output is getting close but looks like
ERWWCOMMUNITIES_Memory_Pages_sec, DBI::st=HASH(0x853cf7c)
ERWWCOMMUNITIES_NetworkInterface_CompaqEthernet_FastEthernetAdapter_Mo
+dule_BytesTotalPERsec, DBI::st=HASH(0x85457e8)
ERWWCOMMUNITIES_NetworkInterface_MSTCPLoopbackinterface_BytesTotalPERs
+ec, DBI::st=HASH(0x8545014)
My script:
#!/usr/bin/perl
#use strict;
use warnings;
use DBI;
use Text::CSV;
# Connect to the database, (the directory containing our csv file(
+s))
my $dbh = DBI->connect("DBI:CSV:f_dir=.;csv_eol=\n;");
# Associate our csv file with the table name 'results'
$dbh->{'csv_tables'}->{'results'} = { 'file' => 'test.csv'};
#my ($avg) = $dbh->selectrow_array("SELECT avg(ERWWCOMMUNITIES_Mem
+ory_Pages_sec) FROM results");
my $file = 'test.csv';
my $csv = Text::CSV->new();
open (CSV, "<", $file) or die $!;
while (<CSV>) {
next if ($. != 1);
if ($csv->parse($_)) {
@columns = $csv->fields();
} else {
my $err = $csv->error_input;
print "Failed to parse line: $err";
}
}
foreach $field (@columns) {
##print ("\$avg \= \$dbh\-\>selectrow_array\(\"SELECT avg\($fi
+eld\) FROM results\"\) \n");
$avg = $dbh->prepare("SELECT avg($field) FROM results");
print ("$field, $avg \n");
}
###print ("$avg,\n");
close CSV;
What am I doing wrong? Is there an easier way than what I've figured out?