Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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?

In reply to DBI::st=HASH output by DrAxeman

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (3)
As of 2024-04-20 05:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found