CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Need Help on Script Original progammer is GONE!

 



xxenclavexx
New User

Nov 8, 2006, 10:11 AM

Post #1 of 4 (424 views)
Need Help on Script Original progammer is GONE! Can't Post

Hello, I really don't know too much about perl, but i do know that we have a perl script that cleans up a montly report where I work. I have added on more line to this monthly report, its a receipt number. I have the script here, as you can see it is fairly short, I would appreciate any help I can get.
Basically this will go through a text document and if any of the designated fields are missing then this moves it to a new file, what I need to figure out is how to make the new field, that I added to the end of the report, get transfered to the new file.

Code
#!c:\perl\bin\perl -wl 

sub trim($) {
my $str = shift(@_);
$str =~ s/(^\s+|\s+$)//g;
return $str;
}

die "You must specify an input file mask." if(scalar(@ARGV) != 1);

@files = glob($ARGV[0]);

foreach $file (@files) {
open(READ,$file) or die "Couldn't open input file.";
@lines = grep(s/\n//,<READ>);
close(READ);

$good_fn = $file;
$bad_fn = $file;
$good_fn =~ s/(\..*$)/\.good$1/;
$bad_fn =~ s/(\..*$)/\.bad$1/;

if($good_fn eq $file) {
$good_fn = ".good.txt";
}

if($bad_fn eq $file) {
$bad_fn = ".bad.txt";
}

open(GOOD,">$good_fn");
open(BAD,">$bad_fn");

foreach $line (@lines) {
if(length(trim(substr($line,94,14))) == 0 || length(trim(substr($line,13,11))) == 0) {
print BAD $line;
next;
}

#
#
$lf = substr($line,45,37);
$lf =~ s/\./0/g;
$line = substr($line,0,45) . $lf . substr($line,82);
# print $line;

# Here we're filling in the NDC field
$lf = trim(substr($line,83,11));
if(length($lf) == 0) {
$lf = "99999999999";
$line = substr($line,0,83) . $lf . substr($line,94);
}

# Fix
$lf = substr($line,108,10);
if(index($lf,".") != -1) {
$lf =~ s/\./0/g;
# print "1:",$line;
# print "2:",substr($line,0,108),$lf,substr($line,118);
$line = substr($line,0,108) . $lf . substr($line,118);
}

#
$lf = trim(substr($line,128,8));
if(length($lf) == 0) {
$lf = "00000000";
$line = substr($line,0,128) . $lf . substr($line,136);
}

#
$lf = substr($line,128,10);
if(length($lf) == 0) {
$lf =~ s/[^A-Za-z0-9]/0/g;
$line = substr($line,0,128) . $lf . substr($line,138);
}

#
$lf = trim(substr($line,168,30));
if(length($lf) == 0) {
$lf = "12345 ";
$line = substr($line,0,168) . $lf . substr($line,198);
}

#
$lf = substr($line,168,30);
if($lf =~ /\_/) {
$lf =~ s/\_/ /g;
$line = substr($line,0,168) . $lf . substr($line,198);
}

#
$lf = trim(substr($line,198,2));
if(length($lf) == 0) {
$lf = "CA";
$line = substr($line,0,198) . $lf . substr($line,200);
}

#
$lf = trim(substr($line,200,5));
if(length($lf) == 0) {
$lf = "99999";
$line = substr($line,0,200) . $lf . substr($line,205);
}

print GOOD $line;
}

close(GOOD);
close(BAD);
}



xxenclavexx
New User

Nov 8, 2006, 1:11 PM

Post #2 of 4 (418 views)
Re: [xxenclavexx] Need Help on Script Original progammer is GONE! [In reply to] Can't Post

Can someone please explain what some of these functions are doing as well, Im having trouble understanding varibles like $line,45,37


davorg
Thaumaturge / Moderator

Nov 9, 2006, 3:49 AM

Post #3 of 4 (413 views)
Re: [xxenclavexx] Need Help on Script Original progammer is GONE! [In reply to] Can't Post

Maybe you'll find the Perl documentation will help.

But really, if you have Perl programs to maintain then your company should either employ a Perl programmer or pay for you you get training (or, at the very least, buy you a couple of books).

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


maximus
Novice

Nov 13, 2006, 1:55 PM

Post #4 of 4 (406 views)
Re: [xxenclavexx] Need Help on Script Original progammer is GONE! [In reply to] Can't Post

Which part are you having problems understanding?

sbstr -> takes the values fromt the defined variable ($line in this case) and reads at the defined starting position for the defined number of characters: i.e $lf = substr($line,0,45) means $lf would now hold the value from $line starting at position 0 and it's length is 45 chars. length just checks for the length of the variable.

The rest of the script is just doing for pattern matching to check the desired results.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives