One of the clients that I work at for my company had a report from an AS400
that was formatted to be printed onto 1099 Tax Forms. This year
they wanted to submit the whole deal electronically for reasons only
known to them. So I looked at the format of the printed out report
and realized that this could only be a job for Perl because it was due
in a week, and to code this in anything other than Perl would just hurt.
I spent two days testing and then once I was happy, I let it rip, and Perl
worked like a charm. I know the code is crufty, and not optimized, but
I was going for dirty get-it-done work. Sadly, this code will never get
used again because they have since moved to a new system, but I thanked Larry
Wall for Perl about a thousand times that week.
open(FILE, 'H:\mer586.txt') or die "Unable to open file: $!\n";
use Win32::OLE;
use Win32::OLE::Const 'Microsoft ActiveX Data Objects';
my $conn = Win32::OLE->new('ADODB.Connection');
$conn->open('1099s');
$loop = 0;
print "Processing....";
while (<FILE>) {
# find the client's taxid, and then look for the payee's taxid
+, either a ssn, or a real taxid
if (/\s*22-222222\s*((\d*\-\d*\-\d*)|(\d+\-\d*)|(\d\d\d\d\d\d\d\d\
+d))/) {
$taxid = $1;
}
# next, the line contained the payee's name and the amount the
+y were paid, so I had to strip that all away
# plus get the whole dollar and cents amount
if (/(\s*|\t*)((([A-Z])*|(\s|\t)*)|(([A-Z])*(\s*|\t*)([A-Z])*))*(\
+t*|\b*|\s*)((\d*)|(\d*\,\d*)|(\d*\,\d*\,\d*))(\.\d*)/) {
$amount = $11;
$amount .= $15;
}
# once I hit the company name, I know I'm ready to grab the ne
+xt set of records....
if (/COMPANY NAME, INC/) { undef $taxid; undef $amount; }
if (($taxid) && ($amount)) { # I only want to insert when I have b
+oth...
$sqlString = "INSERT INTO 1099Data(ProvTaxID, DollarAmount) VA
+LUES (\'$taxid\', \'$amount\');";
$conn->execute($sqlString);
print "\n\n\tUH-OH: ", Win32::OLE->LastError(), "\n" if (Win32
+::OLE->LastError());
exit if (Win32::OLE->LastError());
undef $taxid;
undef $amount;
}
print "." if (++$loop % 300 == 0); # let me know this is still ru
+nning
}
$conn->close if ($conn);
print "Finished!\n";
The thing that was best about it was I got to learn a lot more about regexs and how they work
by the way that the code split up the data and stuck it into the database.