Hi Monks,

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

#! /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 while loop doesn't run over the whole file, it just runs once.

The $record value returned in the debug check is

Current record is |GLOB(0x9d93e48)| 0

In reply to agregating columns in several csv files by Utilitarian

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.