Here's a quick program that does what you request. It doesn't do full CSV parsing (just splitting on commas), but I presume you don't need help with that (use a module). The output isn't lined up vertically either - but that shouldn't matter either I presume.
#!/usr/bin/perl use 5.010; use strict; use warnings; my %data; my %seen; while (<DATA>) { chomp; my ($id, $name, $type, $pid, $qty, $gross) = split /\s*,\s*/; given ($type) { when ('Item') { $data{$id,$name}{$type}{$pid}{qty} += $qty; $data{$id,$name}{$type}{$pid}{gross} += $gross; $seen{$pid} = 1; next; } when ('Cart') { $data{$id,$name}{$type}{qty} = $qty; $data{$id,$name}{$type}{gross} = $gross; } } } my @pids = sort keys %seen; print "Txn ID, Name, Qty, Gross"; print ", $_ Qty, $_ Gross" for @pids; print "\n"; foreach my $ckey (sort keys %data) { my ($id, $name) = split $;, $ckey; print "$id, $name, ", $data{$ckey}{Cart}{qty} // "", ", ", $data{$ckey}{Cart}{gross} // ""; foreach my $pid (@pids) { print ", ", $data{$ckey}{Item}{$pid}{qty} // "", ", ", $data{$ckey}{Item}{$pid}{gross} // ""; } print "\n"; } __DATA__ Txn ID, Name , Type , Product ID, Qty, Gross 010 , Moe , Cart , , 1 , 15 010 , Moe , Item , widget-1 , 1 , 15 020 , Larry, Cart , , 3 , 35 020 , Larry, Item , widget-1 , 1 , 15 020 , Larry, Item , widget-2 , 2 , 20
Output:
Txn ID, Name, Qty, Gross, widget-1 Qty, widget-1 Gross, widget-2 Qty, +widget-2 Gross 010, Moe, 1, 15, 1, 15, , 020, Larry, 3, 35, 1, 15, 2, 20

In reply to Re: How to combine rows of a spreadsheet by JavaFan
in thread How to combine rows of a spreadsheet by wrinkles

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.