Homerhrdz has asked for the wisdom of the Perl Monks concerning the following question:

I have an input file that comes in like this

|M737|5555|Jun 05,2010|753|4562| |M737|5555|Jun 05,2010|123|4562| |Q569|5555|Jun 05,2010|584|4562| |Q569|5555|Jun 05,2010|345|4562| |Q569|5555|Jun 05,2010|215|4562| |Q569|5555|Jun 05,2010|845|4562| |M729|5652|Jun 11,2010|198|4562| |M729|5876|Jun 15,2010|298|4562| |M729|5726|Jun 18,2010|428|4562| |M729|5147|Jun 20,2010|918|4562| |M729|5632|Jun 01,2010|938|4562|
But i would like to use perl to sort and add the last two colums and break it out like this
|M729|5555|Jun 05,2010|498|4562| |M729|5555|Jun 06,2010|978|4562| |M729|5632|Jun 01,2010|938|4562| |M729|5652|Jun 11,2010|198|4562| |M729|5726|Jun 18,2010|428|4562| |M729|5876|Jun 15,2010|298|4562| there were $A m729s and total of 2nd to last row was 898453 and total +of last row was 78665156 ###I just made up those total numbers |M737|5555|Jun 05,2010|123|4562| |M737|5555|Jun 05,2010|563|4562| |M737|5555|Jun 05,2010|725|4562| |M737|5555|Jun 05,2010|753|4562| |M737|5555|Jun 07,2010|913|4562| |M737|5555|Jun 08,2010|921|4562| there were $A m737s and total of 2nd to last row was 898453 and total +of last row was 78665156 ###I just made up those total numbers |Q569|5555|Jun 05,2010|215|4562| |Q569|5555|Jun 05,2010|345|4562| |Q569|5555|Jun 05,2010|457|4562| |Q569|5555|Jun 05,2010|584|4562| |Q569|5555|Jun 05,2010|785|4562| there were $A m737s and total of 2nd to last row was 898453 and total +of last row was 78665156 ###I just made up those total numbers

I would appreciate the helps thanks guys

Replies are listed 'Best First'.
Re: Sorting and Arranging data from input file
by toolic (Bishop) on Jun 10, 2010 at 23:25 UTC
    Perl is an excellent choice for this task. Read some documentation Then write some code. If you still have problems, post what you have tried (in code tags -- refer to Writeup Formatting Tips).
Re: Sorting and Arranging data from input file
by choroba (Cardinal) on Jun 10, 2010 at 21:47 UTC
      This is what i have tried
      #!/usr/bin/perl my $inFile = "input file"; my $outFile = "output file"; open IN, "< $inFile"; open OUT, ">> $outFile"; my @not_sorted = <IN>; print OUT "|CSED Form|OrderNumber|Date|Total Documents|Total Pages|\n +"; @sorted = sort { lc($a) cmp lc($b) } @not_sorted; # alphabetic +al sort my $a = 0; while ($lstxt = pop @sorted) { $field[0] = $lstxt; $field[0] = substr $lstxt, 1, 3; $field[0] = $form; if ($form eq "M729") { print OUT "$lstxt"; $a++; } else { print OUT "There were $a Monthly 729 runs\n"; } $a = 0; if ($form eq "M737") { print OUT "$lstxt"; $a++; } else{ print OUT "there were $a Monthly 737 runs\n"; } $a = 0; if ($form eq "Q569") { print OUT "$lstxt"; } else{ print OUT "there were $a Quarterly 569 runs\n"; } } close OUT; close IN;
        warnings would have pointed you to a bug in your code: you never assign a value to $form.

        Other suggestions: