The melt and cast functions of the Data::Table module are useful for these kinds of data aggregation tasks. Assuming that your data.csv file is in a valid CSV format such as,
Amount,ChoiceId,Allowed PID,AmountId,MultiplyingFactor,PF new,ServiceT +ax,ExtraTalkTime,MRP,Recharge Type,FRC/SRC 13,na,285,100,1.1236,81.75,0.02,0,92,Freebie(BA)+PID,Normal 59,na,285,101,1.1236,8.31,0.07,0,10,Freebie(BA)+PID,Normal 13,na,289,100,1.1236,81.75,0.05,0,92,Freebie(BA)+PID,Normal 2001,na,285,103,1.103,27.13,2.06,0,52,MA+DataPack+PID,Normal 1572,na,285,104,na,0,na,na,na,"Blank MRP,rest OK",Normal 1574,na,285,105,na,0,na,na,na,"Blank MRP,rest OK",Normal 333300,na,285,106,1,0,0,0,3333,Freebie(BA)+MA+PID ,Normal 93,na,285,107,1.1236,100.53,0.11,0,114,Freebie(BA)+PID+SA,Normal 78,na,285,108,1.1236,8.12,0.1,0,10,Freebie(BA)+PID,Normal 79,na,285,109,1.1236,85.54,0.1,0,97,BA20 only,Normal 13,na,290,100,1.1236,81.75,0.11,0,92,Freebie(BA)+PID,Normal
You can get the output you desire like this,
#!/usr/bin/env perl use strict; use warnings; use Data::Table; my $dt = Data::Table::fromCSV("data.csv"); # print "DATA:\n"; # print $dt->csv; # All of the columns except the ones you want to aggregate my $cols = [ 'Amount', 'AmountId', 'MRP', 'ChoiceId', 'MultiplyingFactor', 'PF new', 'ExtraTalkTime', 'Recharge Type', 'FRC/SRC' ]; my $mdt = $dt->melt( $cols ); # print "MELT:\n"; # print $mdt->csv; my $cdt = $mdt->cast( $cols, 'variable', Data::Table::STRING, 'value', \&aggregate ); #print "CAST:\n"; #print $cdt->csv; exit; sub aggregate { my @values = @_; my $av = join(';', @values); return($av); }
One can uncomment the print statements to see the intermediate tables. The final result in $cdt is:
Amount,AmountId,MRP,ChoiceId,MultiplyingFactor,PF new,ExtraTalkTime,Re +charge Type,FRC/SRC,Allowed PID,ServiceTax 13,100,92,na,1.1236,81.75,0,Freebie(BA)+PID,Normal,285;289;290,0.02;0. +05;0.11 59,101,10,na,1.1236,8.31,0,Freebie(BA)+PID,Normal,285,0.07 2001,103,52,na,1.103,27.13,0,MA+DataPack+PID,Normal,285,2.06 1572,104,na,na,na,0,na,"Blank MRP,rest OK",Normal,285,na 1574,105,na,na,na,0,na,"Blank MRP,rest OK",Normal,285,na 333300,106,3333,na,1,0,0,Freebie(BA)+MA+PID ,Normal,285,0 93,107,114,na,1.1236,100.53,0,Freebie(BA)+PID+SA,Normal,285,0.11 78,108,10,na,1.1236,8.12,0,Freebie(BA)+PID,Normal,285,0.1 79,109,97,na,1.1236,85.54,0,BA20 only,Normal,285,0.1

In reply to Re: perl: How to comapre and manipulate csv file in perl? by kevbot
in thread perl: How to comapre and manipulate csv file in perl? by Ankur_kuls

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.