cesear has asked for the wisdom of the Perl Monks concerning the following question:
I need remove lines of file where string 406 or 408 is found in the line based on a key. Example of a line to remove is:
4201034600051 1212104069140 001The key is 1034600051 the account number. The string to check for is 406 is the rev code.
A sample input file looks like this:
201034600051 1212104069140 001 + 4201034600051 1212104039139 001 + 4201034600153 1212104039140 001 + 4201034000375 1212104034111 001 + 4201034000375 1212104033180 001 + 4201034000375 1212104039443 001 + 4201034000375 1212104039232 001 + 4201034600039 1212104039336 001 + 4201034600045 1212104039252 001
Output codes looks like this
CH 01034600051 1212104069140 001 LAB CH 01034600051 1212104039139 001 LAB CH 01034600153 1212104039140 001 LAB CH 01034000375 1212104034111 001 LAB CH 01034000375 1212104033180 001 LAB CH 01034000375 1212104039443 001 LAB CH 01034000375 1212104039232 001 LAB CH 01034600039 1212104039336 001 LAB CH 01034600045 1212104039252 001 LAB
I don't want the line CH 01034600051 1212104069140 001 LAB to be returned because rev code 406 was found
So, I batch up lines where account is the same (can occur mulitple times) and if the line contains 406 or 408 rev code I need to remove the line and not send to output file. Here is the code I have written so far:use warnings; use strict; my $file_to_process = $ARGV[0]; my $file_to_create = $ARGV[1]; my $curr_acctn = {}; my $first_time = "yes"; my @account_list = (); 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); if ($chrg_credit == 42) { substr($account,0,2) = "CH "; substr($account,32,3) = " LAB"; } else { 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"; print FH1 "$account\n"; } @account_list = (); } } $curr_acctn = $acctn; push (@account_list, $_); } close FH; close FH1; sub rtrim() { $_ =~ s/\s+$//; return; }
I add some other things to the output and remove trailing spaces. I just can not figure out how to get rid of the lines with 406 or 408 rev codes
THANKS
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Removing element of an array
by kennethk (Abbot) on Apr 14, 2011 at 15:49 UTC | |
by cesear (Novice) on Apr 14, 2011 at 16:07 UTC | |
|
Re: Removing element of an array
by tospo (Hermit) on Apr 14, 2011 at 15:43 UTC | |
|
Re: Removing element of an array
by davido (Cardinal) on Apr 14, 2011 at 15:58 UTC | |
|
Re: Removing element of an array
by merlininthewood (Initiate) on Apr 14, 2011 at 15:59 UTC |