Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^3: Advances SQL calculations in CSV file

by Roger (Parson)
on Aug 31, 2005 at 18:01 UTC ( #488175=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Advances SQL calculations in CSV file
in thread Advances SQL calculations in CSV file

SQL is the unnecessary complexity you don't want in such a simple task. Besides DBD::CSV implements a very limited subset of SQL.

What you should do is just to have a single pass over your file reading a line at the time, build your hash table of statistics along the way while splitting the records, and print out the stats at the end.

my %stats; open MYFILE, "<data.txt" or die "Can not open file: $!"; chomp(my $heading = <MYFILE>); my @col = split /,/, $heading; while (my $line = <MYFILE>) { # build a hash for the input record chomp($line); my @rec = split /,/, $line; my %rec; @rec{@col} = @rec; # collect stats $stats{$rec{rec_id}}{BAL} += $rec{bal}; $stats{$rec{rec_id}}{NUM} ++; } # print the stats here for my $id (keys %rec) { if ($rec{$id}{NUM}) { print "$id => ", $rec{$id}{BAL}/$rec{$id}{NUM}, "\n" } } close MYFILE;


Comment on Re^3: Advances SQL calculations in CSV file
Download Code
Re^4: Advances SQL calculations in CSV file
by DrAxeman (Beadle) on Aug 31, 2005 at 19:52 UTC
    I'm getting the following error, but everything looks initialized to me..

    Use of uninitialized value in hash element at ./top.pl line 19, <MYFIL +E> line 994. Use of uninitialized value in addition (+) at ./top.pl line 19, <MYFIL +E> line 994. Use of uninitialized value in hash element at ./top.pl line 20, <MYFIL +E> line 994.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2014-09-02 10:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (21 votes), past polls