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

Hmmm, I did it this way because much of what I am doing to the data it not pertinent to this problem. I can see what you mean about showing and not telling. I'll see what I can come up with, thanks.

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

Replies are listed 'Best First'.
Re^3: Accumulate values for each data field
by GrandFather (Saint) on Feb 10, 2016 at 00:59 UTC

    Read I know what I mean. Why don't you?. We don't want the whole thing, just like we don't want all the data. We want to see just the code you are having trouble with wrapped up in to tiny test framework with just enough data to show the issue.

    Premature optimization is the root of all job security

      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.

        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