Hi, I was given a file to reformat, the sample is shown below:

MSC_name,chunum,date,count
MSCBCR1,1,01-JUL-03,168
MSCBCR1,1,02-JUL-03,163
MSCBCR1,1,03-JUL-03,166
MSCBCR1,1,04-JUL-03,159
MSCBCR1,1,05-JUL-03,161
MSCBCR1,1,06-JUL-03,161
MSCBCR1,1,07-JUL-03,159
MSCBCR1,1,08-JUL-03,158
MSCBCR1,1,09-JUL-03,160
MSCBCR1,1,10-JUL-03,161
MSCBCR1,1,11-JUL-03,164
MSCBCR1,1,12-JUL-03,166
MSCBCR1,1,13-JUL-03,165
MSCBCR1,1,14-JUL-03,160
MSCBCR1,1,15-JUL-03,163
MSCBCR1,1,16-JUL-03,170
MSCBCR1,1,17-JUL-03,162
MSCBCR1,1,18-JUL-03,161
MSCBCR1,1,19-JUL-03,165
MSCBCR1,1,20-JUL-03,162
MSCBCR1,1,21-JUL-03,155
MSCBCR1,1,22-JUL-03,160
MSCBCR1,1,23-JUL-03,161
MSCBCR1,1,24-JUL-03,159
MSCBCR1,1,25-JUL-03,160
MSCBCR1,1,26-JUL-03,161
MSCBCR1,1,27-JUL-03,170
MSCBCR1,1,28-JUL-03,170
MSCBCR1,1,29-JUL-03,170
MSCBCR1,1,30-JUL-03,170
MSCBCR2,1,01-JUL-03,168
MSCBCR2,1,02-JUL-03,163
MSCBCR2,1,03-JUL-03,166
MSCBCR2,1,04-JUL-03,159
MSCBCR2,1,05-JUL-03,161
MSCBCR2,1,06-JUL-03,161
MSCBCR2,1,07-JUL-03,159
MSCBCR2,1,08-JUL-03,158
MSCBCR2,1,09-JUL-03,160
MSCBCR2,1,10-JUL-03,161
MSCBCR2,1,11-JUL-03,164
MSCBCR2,1,12-JUL-03,166
MSCBCR2,1,13-JUL-03,165
MSCBCR2,1,14-JUL-03,160
MSCBCR2,1,15-JUL-03,163
MSCBCR2,1,16-JUL-03,170
MSCBCR2,1,17-JUL-03,162
MSCBCR2,1,18-JUL-03,161
MSCBCR2,1,19-JUL-03,165
MSCBCR2,1,20-JUL-03,162
MSCBCR2,1,21-JUL-03,155
MSCBCR2,1,22-JUL-03,160
MSCBCR2,1,23-JUL-03,161
MSCBCR2,1,24-JUL-03,159
MSCBCR2,1,25-JUL-03,160
MSCBCR2,1,26-JUL-03,161
MSCBCR2,1,27-JUL-03,170
MSCBCR2,1,28-JUL-03,170
MSCBCR2,1,29-JUL-03,170
MSCBCR2,1,30-JUL-03,170
The output that will be needed will be:
MSC_name,count_per_date1...end_date(e.g. 30-JUL-03)
total_cf_produced,total_per_date...total_per_date(e.g. end_date 30-JUL-03
I've been trying out this code but can't get it to work:

#!c:/perl/bin/perl open (INFILE,"c:/playgnd/sql_result_cf.txt") || die("Could not open fi +le!"); while( <INFILE> ) { next if $. == 1; # skip first line chomp; my %F; @F{qw(msc_name chunum date count_per_date)} = split /\s*,\s*/; $Data{$F{date}} = { } unless exists $Data{$F{msc_name}}; $Data{$F{date}}->{$F{msc_name}} = [('')x5] unless ref( $Data{$F{date}}->{$F{msc_name}}); ${$Data{$F{date}}->{$F{msc_name}}}[$F{hour}] = $F{count_per_hour}; } foreach my $date (keys %Data) { foreach my $msc_name (keys %{$Data{$date}}) { print join(",", $date, $msc_name, @{$Data{$date}->{$msc_name}}), +"\n" ; } } close(INFILE);

Edit: Added <code> tags, larsen.


In reply to Formating Text file by redskie007

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.