Here is my entire code:
use warnings; use strict; use diagnostics; my $file_to_process = $ARGV[0]; my $file_to_create = $ARGV[1]; my $curr_acctn = 0; my $first_time = "yes"; my @account_list = (); my $alpha_chrg_credit = ''; my %bundle; open FH1, ">$file_to_create" or die "Cannot create $file_to_create"; open FH, $file_to_process or die "Cannot open $file_to_process"; while (<FH>) { chomp; &rtrim($_); my @fields = split; my $acctn = substr($fields[0],3,10); if($acctn != $curr_acctn) { if($first_time eq "yes") { $first_time = "no"; } else { #print FH1 "**************************\n"; #print FH1 "Charges for account number ----> $curr_acctn:\n"; foreach my $account (@account_list) { my $charge_code = substr($account,20,7); my $chrg_credit = substr($account,0,2); my $date = substr($account,14,6); if ($chrg_credit == 42) { $alpha_chrg_credit = "CH"; substr($account,0,2) = "CH "; substr($account,32,3) = " LAB"; } else { $alpha_chrg_credit = "CR"; substr($account,0,2) = "CR "; substr($account,32,3) = " LAB"; } #print FH1 "The charge or credit is: $chrg_credit\n"; #print FH1 "The charge code is: $charge_code\n"; if ($account !~ /.{21}40[68]/) { if ($charge_code != 4039252 && $charge_code != 4039139 & +& $charge_code != 4039140) { print FH1 "$account\n"; } } if ($charge_code == 4039252) { $account = ""; $account = "$alpha_chrg_credit 0$curr_acctn ${date}40331 +58 001 LAB\n$alpha_chrg_credit 0$curr_acctn ${date}4033240 001 LAB\n$ +alpha_chrg_credit 0$curr_acctn ${date}4039241 001 LAB"; print FH1 "$account\n"; } if ($charge_code == 4367958) { $account = ""; $account = "$alpha_chrg_credit 0$curr_acctn ${date}43679 +58 001 LAB\n$alpha_chrg_credit 0$curr_acctn ${date}4367959 001 LAB"; print FH1 "$account\n"; } #if ($charge_code == 4039139) { # $bundle{$charge_code} += 1; # foreach my $key (sort keys %bundle) { # print "$account contained...$key => $bundle{$key}\n" +; #} #} if ($charge_code == 4039140) { $bundle{$charge_code} += 1; foreach my $key (sort keys %bundle) { print "$account contained...$key => $bundle{$key}\n"; + } } } @account_list = (); undef %bundle; } } $curr_acctn = $acctn; push (@account_list, $_); } close FH; close FH1; sub rtrim() { $_ =~ s/\s+$//; }
The part I am stuck on is here:
if ($charge_code == 4039140) { $bundle{$charge_code} += 1; foreach my $key (sort keys %bundle) { print "$account contained...$key => $bundle{$key}\n"; + } }

Here is the logic I need to add

if all of these records ($charge_code) are present for a particular account ($acctn), they are deleted (not written out) and a record with a new charge code is created. 4039139 and 4039140 ==> 4039142 (new charge code) If 4039139 or 4039140 are found by themselves then just write out. If they are found together, then create a new charge line per grouping. I starting building a hash for these grouping, but I am sure to write it out per this logic.

So an input file could be this

4201034600031 1212104034111 001 + 4201034600031 1212104033180 001 + 4201034600031 1212104039140 001 + 4201034600031 1212104039139 001 + 4201034600031 1212104039463 001 + 4201034600031 1212104039295 001 + 4201034600031 1212104039140 001 + 4201034600031 1212104039139 001 + 4201034600031 1212104045022 001 + 4201034600031 1212104045019 001 + 4201034600031 1212104045022 001

and output file should look like this

CH 01034600031 1212104034111 001 LAB CH 01034600031 1212104033180 001 LAB CH 01034600031 1212104039142 001 LAB CH 01034600031 1212104039463 001 LAB CH 01034600031 1212104039295 001 LAB CH 01034600031 1212104039142 001 LAB CH 01034600031 1212104045022 001 LAB CH 01034600031 1212104045019 001 LAB CH 01034600031 1212104045022 001 LAB
THANKS PERLMONKS

In reply to hash help by cesear

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.