DrAxeman has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl use strict; use warnings; use DBI; use Text::CSV; use Data::Types qw(:all); # 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 $sth = $dbh->prepare("SELECT * FROM results WHERE 1=0"); $sth->execute; my @cols = @{$sth->{NAME}}; shift @cols; my $avgSQL = 'SELECT ' . join(', ', map { "avg($_) \n" } @cols ) . ' FROM results'; my @avgs = $dbh->selectrow_array($avgSQL); my %avgsHash; @avgsHash{ @cols } = @avgs; # uses a hash slice to populate %avgs +Hash print "Col,Avg\n"; printf("%s,%f\n", $_, $avgsHash{$_}) for @cols;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Pre-process csv files before using
by jZed (Prior) on Aug 06, 2005 at 18:12 UTC | |
|
Re: Pre-process csv files before using
by sk (Curate) on Aug 06, 2005 at 18:19 UTC | |
by DrAxeman (Scribe) on Aug 06, 2005 at 18:40 UTC | |
by sk (Curate) on Aug 06, 2005 at 18:51 UTC | |
by DrAxeman (Scribe) on Aug 06, 2005 at 18:58 UTC | |
by davidrw (Prior) on Aug 06, 2005 at 19:26 UTC | |
by sk (Curate) on Aug 06, 2005 at 20:34 UTC | |
| |
by sk (Curate) on Aug 06, 2005 at 19:20 UTC | |
|