rendered paste body#!/usr/bin/perl -w -s
open FILE, "file.txt" or die $!;
my $line_num = 0;
my $FLAG = 0;
my $compile = 0;
my $error = 0;
while (my $line = <FILE>)
{
$line_num++;
chomp($line);
my $index = substr($line, 0, 5);
if ($FLAG == 1) {
$line=~/CUST#:.+?(\w.+)PMT\$:\s+(.+)\s/;
if (!defined $1 || $1 eq "000000") {
$error = 1;
next; #didn't provide cust #
}
if (!defined $2) {
$error = 1;
next; #no payment?
}
$custnum = $1;
$payment = $2;
$FLAG = 0;
$compile = 1;
}
if ($index eq "REMIT") {
$line=~/TTER:\s(.+?)\s+CK#:.+?([1-9].+)\sCK/;
if (!defined $1) {
$error = 1;
next; #jump to next record. Can't compare to cust #
}
if (!defined $2) {
$2 = "1234"; #check # not found. Give one instead
}
$name = $1;
$cknum = $2;
$FLAG = 1;
}
if ($compile == 1) {
push(@names, $name);
push(@cknums, $cknum);
push(@custnums, $custnum);
push(@payments, $payment);
$compile = 0;
}
}
continue {
if ($error == 1) {
$line=~s/^\s+|\s+$//g; #Sanatize line
push(@errors, "Error: at line ".$line_num.": ".$line."\n");
$error = 0;
}
}
open RESULT, ">output.txt";
my $total = 0;
for ($i=0;$i<scalar(@payments);$i++) {
$total = $payments[$i] + $total;
}
print RESULT $total."\n";
for ($i=0;$i<scalar(@names);$i++) {
print RESULT "$names[$i],$cknums[$i],$custnums[$i],$payments[$i]\n";
}
for ($i=0;$i<scalar(@errors);$i++) {
print "$errors[$i]\n";
}
close FILE;
close RESULT;