All pastes #1461124 Raw Edit

Anonymous

public text v1 · immutable
#1461124 ·published 2009-06-15 14:42 UTC
rendered paste body
#!/usr/bin/perl -w 

open FILE, "file.txt" or die $!;
my $line_num = 0;
my $FLAG = 0;

my @custnum;
my @payment;
my @names; 
my @cknum; 
my @errors;

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 && defined $2) {
                push(@custnum, $1);
                push(@payment, $2);
        }
        else {
            push(@errors, "Error at line ".$line_num."\n\t".$line."\n");
        }

        $FLAG = 0;
    }

    if ($index eq "REMIT") { # Catch a line to extract info from. Then place flag for next line 
        $line=~/TTER:\s(.+?)\s+CK#:.+?([1-9].+)\sCK/;
        
        if (defined $1 && defined $2) { 
            $name = $1;
            push(@names, $1);
            push(@cknum, $2);
            $FLAG = 1;
        }
        else {
            if (!defined $2) { #Place bogus cknum if emtpy
                push(@cknum, "1234");
                push(@names, $name);
                $FLAG = 1;
            }
            else {
                push(@errors, "Error at line ".$line_num."\n\t".$line."\n");
            }
        }
    }
}

open RESULT, ">output.txt";
open ERROR, ">error.txt";

for ($i=0;$i<scalar(@names);$i++)
{
    print RESULT "$names[$i],$custnum[$i],$cknum[$i],$payment[$i]\n";
}

for ($i=0;$i<scalar(@errors);$i++)
{
    print ERROR "$errors[$i]\n";
}

close FILE;
close RESULT;
close ERROR;