Utilitarian has asked for the wisdom of the Perl Monks concerning the following question:
I have ~3K csv files that I need to aggregate the records of, (sum some columns, find the max for others). The following code fails for some, probably blindingly obvious, reason I have failed to spot. @ARGV is a list of csv files in the current directory, all of which contain the same number of records.
What I thought I wrote
open all files in @ARGV assigning a file handle to each in the @FH array.
While there are unread lines in the first file
The while loop doesn't run over the whole file, it just runs once.#! /usr/bin/perl use strict; use warnings; #GLOBALS my $DEBUG=1; # 0 no debug messages, 1 light logging, >1 lots of loggin +g my (@FH,$TOTALS); for my $index (0..$#ARGV){ open ($FH[$index],"<","$ARGV[$index]")||die "cant open $ARGV[$index +] $!\n"; print "assigned ",$index-1," to $ARGV[$index]\n if $DEBUG;"; } open($TOTALS ,">","totals.csv"); while (<{$FH[0]}>){ chomp; print "Current record is |$_|" if $DEBUG; my @totals=split; #space seperated for my $FH (@FH[1..$#FH]){ my $record=readline($FH)||warn "cannot read file $!\n"; chomp($record); print "$record\n" if ($DEBUG>1); my @record=split(/ +/,$record); $totals[1]+=$record[1];#total read volume $totals[2]+=$record[2];#total write volume $totals[3]=$record[3] if $totals[3]<$record[3];#Return max respo +nse time $totals[4]=$record[4] if $totals[4]<$record[4];#Return max % wai +ting $totals[5]=$record[5] if $totals[5]<$record[5];#Return max % blo +cked $totals[6]+=$record[6]; # total errors $totals[7]+=$record[7]; # total errors $totals[8]+=$record[8]; # total errors $totals[9]+=$record[9]; # total errors $totals[10]+=$record[10]; # total errors $totals[11]+=$record[11]; # total errors print "totalling record $record[0] for $#FH files\n" if $DEBUG; } print $TOTALS "@totals\n"; }
The $record value returned in the debug check is
Current record is |GLOB(0x9d93e48)| 0
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: agregating columns in several csv files
by Anonymous Monk on Jun 05, 2009 at 07:58 UTC | |
by Utilitarian (Vicar) on Jun 05, 2009 at 08:09 UTC | |
|
Re: agregating columns in several csv files
by Corion (Patriarch) on Jun 05, 2009 at 07:50 UTC | |
by Utilitarian (Vicar) on Jun 05, 2009 at 07:58 UTC | |
by Corion (Patriarch) on Jun 05, 2009 at 08:03 UTC | |
by Anonymous Monk on Jun 05, 2009 at 08:04 UTC | |
|
Re: agregating columns in several csv files
by NiJo (Friar) on Jun 06, 2009 at 10:40 UTC |