Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^2: Combine data in table

by DrAxeman (Beadle)
on Aug 07, 2005 at 18:02 UTC ( #481707=note: print w/ replies, xml ) Need Help??


in reply to Re: Combine data in table
in thread Combine data in table

This is what I did:

# Connect to the database 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' => "file.csv"}; my $row = "IP,Partition,PartitionSize,PartitionFree"; my %ips; foreach my $row (@$row){ $ips{ $row->{colIP} }->{ colPartitionSize } += $row->{colPartitionSi +ze}; $ips{ $row->{colIP} }->{ colPartitionFree } += $row->{colPartitionFr +ee}; } print Dumper \%ips;

But I am getting:
Can't use string ("IP,Partition,PartitionSize,Parti") as an ARRAY ref +while "strict refs" in use at ./psfile.pl line 39.


Comment on Re^2: Combine data in table
Select or Download Code
Re^3: Combine data in table
by davidrw (Prior) on Aug 07, 2005 at 18:09 UTC
    I would recommend using the "SELECT ... GROUP BY" SQL statement -- it's very conscise and exactly what you want, and leverages the fact you're using DBD/CSV ..


    As for your code in this post, though, you have my $row = "IP,Partition,PartitionSize,PartitionFree"; which just sets some string as the value for $row .. you need it to be all the data as an array of hashrefs ..
    my $rows = $dbh->selectall_arrayref("SELECT * FROM your_table", {Slice +=>{}}, ); my %ips; foreach my $row (@$rows){ $ips{ $row->{colIP} }->{ colPartitionSize } += $row->{colPartitionSi +ze}; $ips{ $row->{colIP} }->{ colPartitionFree } += $row->{colPartitionFr +ee}; } print Dumper \%ips;
    (Note that this is inefficient, as well -- would be better to use a $sth and fetch one row at a time instead of sucking in all the rows at once)
      I got the Perl loop method to work (mostly for the experience). But now I'm working on the SQL method. My code looks like:

      # 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' => "psinfooutputdiskfile. +csv"}; my @rows = $dbh->selectrow_arrayref("SELECT IP, SUM(PartitionSize), SU +M(PartitionFree) FROM results GROUP BY IP"); print ("@rows\n"); # Just here to view output

      I'm getting

      SQL ERROR: Can't find table names in FROM clause! Execution ERROR: No command found!.

      Not sure what is wrong here.

      it I modify the SELECT statement to:
      my @rows = $dbh->selectrow_arrayref("SELECT IP FROM results");

      It works. But if I try to include a GROUP BY or SUM I get the error.
        Note first that you probably want my $rows = $dbh->selectall_arrayref( ... ) instead of selectrow_arrayref()

        are those column names right? should they be "colIP", "colPartitionSize", "colPartitionFree" instead?

        Besides that, the SQL looks fine .. do the statements "SELECT IP FROM results" and "SELECT IP FROM results GROUP BY IP" and "SELECT SUM(PartitionSize), SUM(PartitionFree) FROM results" work?
      Using this method, how would I print out %ips? I'd like data comming out to like the original data:

      IP,PartitionFree,PartitionSize 1.2.3.4,23452,234322 2.3.4.5,99348,8666623
        here's three different ways.. note that the second two make it trivial to output in a sorted manner (something like sort keys %ips instead of just keys %ips):
        while( my ($ip, $h) = each %ips ){ printf "%s,%d,%d\n", $ip, $h->{colPartitionSize}, $h->{colPartitionF +ree}; } foreach my $ip ( keys %ips ){ my $h = $ips{$ip}; printf "%s,%d,%d\n", $ip, $h->{colPartitionSize}, $h->{colPartitionF +ree}; } printf "%s,%d,%d\n", $_, $ips{$_}->{colPartitionSize}, $ips{$_}->{colP +artitionFree} for keys %ips;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://481707]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (17)
As of 2014-12-18 14:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (56 votes), past polls