in reply to Re^3: Accumulate values for each data field
in thread Accumulate values for each data field

I updated the original post a bit BEFORE I took the time to read your post. Sorry! I think I understand what you are talking about with the _DATA_ section. I have never used this before, so I'll go play with it now. It would simplify showing the problem I am having.

  • Comment on Re^4: Accumulate values for each data field

Replies are listed 'Best First'.
Re^5: Accumulate values for each data field
by poj (Abbot) on Feb 12, 2016 at 12:09 UTC

    The one liner has 2 loops. The first loops through the records accumulating the column 2 value by column 4 value into a hash. The second loops through the hash printing the key/value pairs. Here's what I think you are trying to do

    #!perl use strict; use Text::CSV_XS; use File::BOM; use Data::Dump 'pp'; my $infile = $ARGV[0] || 'c:/temp/data.csv'; my $outfile = 'c:/temp/ModCSV.csv'; open my $ORIG_CSV, '<:via(File::BOM)', $infile or die "Can't open $infile: $!"; open my $MOD_CSV, '>:raw:encoding(iso-8859-1)', $outfile or die "Can't open $outfile: $!"; my $aCSV = Text::CSV_XS->new({ eol => undef, # \r, \n, or \r\n quote => '"', binary => 1, allow_whitespace => 1, allow_loose_quotes => 1, }); my %i=(); # used to summate my @rows; my %count=(); # used to remove duplicates while (my $row = $aCSV->getline($ORIG_CSV)) { if ($row->[0] =~ /\S/ ) { my $key = $row->[3]; if ($row->[1] =~ /^\d+/) { $i{$key} += $row->[1]; } # remove last 2 columns splice @$row,-2; # only save 1 record for each key push @rows, $row unless ($count{$key}++); } } close $ORIG_CSV; # show the summation for (sort keys %i){ print "$i{$_} $_\n"; } # update @rows with sum my $n=0; for (@rows){ my $key = $_->[3]; next if $key eq 'textbox85'; # skip header $_->[1] = $i{$key}; $_->[0] = ++$n; # re-number } # output @rows $aCSV->say($MOD_CSV, $_) for @rows;
    poj
      "The one liner has 2 loops. The first loops through the records accumulating the column 2 value by column 4 value into a hash. The second loops through the hash printing the key/value pairs."

      This is makes more sense now. Thanks for the reply. Now off to test some changes.