This code should work. It creates a hash, the part number being the key. The value is another hash, containing two keys, num and des, whose values are the quantity and the list of designators.
#!/usr/bin/perl use strict; use warnings; my %parts; while(my $line = <DATA>){ chomp $line; $line =~ s/"//g; my ($part,$num,@des) = split /,/,$line; $parts{$part}{num} += $num; $parts{$part}{des} = {} unless exists $parts{$part}{des}; my %des = %{ $parts{$part}{des} }; @des{@des} = (); %{ $parts{$part}{des} } = %des; } foreach my $part_id (sort keys %parts){ my $part = $parts{$part_id}; my $des_string = join ',', sort keys %{ $part->{des} }; $des_string = '"'.$des_string.'"' if $des_string =~ /,/; print join ',',$part_id,$part->{num},$des_string; print "\n"; } __DATA__ 032-00751-0000,1,R383 032-00794-0000,6,"RP1,RP2,RP3,RP22,RP24,RP26" 032-00795-0000,8,"RP10,RP11,RP12,RP13,RP14,RP15,RP16,RP17" 032-00804-0000,7,"R7,R14,R21,R23,R41,R42,R49" 032-00807-0000,2,"RP18,RP19" 032-00807-0000,4,"RP8,RP9,RP200,RP201" 032-00808-0000,3,"RP21,RP23,RP25" 032-00820-0000,5,"R966,R970,R971,R1041,R1076" 032-00820-0000,1,R3000 032-00893-0000,1,R1164

Update: designators sorted.


In reply to Re: Find duplicate fields and merging data in a text file by choroba
in thread Find duplicate fields and merging data in a text file by donkost

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.