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

In reply to Re^5: Accumulate values for each data field by poj
in thread Accumulate values for each data field by JobC

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.