G
Ghee
Hello,
I've written a short script to read in a form with HTML-style tags
(form is postscript, can be 0.5MB to 20MB+, tags are alphanumerical
wiht - or _ in), and substitute values into these tags from a separate
data file (pipe delimited, ie data|value), writing out the populated
form as a new file.
The tags will appear in the form enclosed in angled brackets, within a
postscript "text object", which is delimited by rounde brackets - eg:
.........(....<TAG1>...<TAG2>....)......
The following script works, but I've got the feeling that it's not
very efficient. So, can any of you suggest improvements over my
perl-newbie script?
#!/usr/bin/perl
#open output file, tag data, and the form
open (OUTPUT,">OUTPUT.ps") || die "can't open output file";
open (DATA,"DATA.PDF") || die "can't open data file";
open (FORM,"FORM1.ps") || die "can't open form file";
#Read in pipe delimited file (PDF), put data in associative array
@data=<DATA>;
foreach $item (@data) {
@items=split /\|/,$item;
$variable=$items[0];
$value=$items[1];
$$variable=$value; chop $value;
$array{$variable}=$value;
};
#read through form line by line, identifying any tags and replacing
#values from associative array as neccesary
while(<FORM>) {
$stuff = $_;
$done_stuff = "";
while ($stuff =~ /<[0-9a-zA-Z-_]+>/) {
$done_stuff = $done_stuff . $`; #write out pre match
$stuff = $'; #move post match to $stuff to loop again...
$tag_name = $&;
$tag_name =~ s/<|>//g; #take off < and >
if (exists $array{$tag_name}) { #replace if exists in array
$done_stuff = $done_stuff . $array{$tag_name};
} else {
$done_stuff = $done_stuff . "<$tag_name>";
}
}
$done_stuff = $done_stuff . $stuff;
print OUTPUT $done_stuff;
};
close (FORM);
close (DATA);
close (OUTPUT);
Regards,
Ghee
I've written a short script to read in a form with HTML-style tags
(form is postscript, can be 0.5MB to 20MB+, tags are alphanumerical
wiht - or _ in), and substitute values into these tags from a separate
data file (pipe delimited, ie data|value), writing out the populated
form as a new file.
The tags will appear in the form enclosed in angled brackets, within a
postscript "text object", which is delimited by rounde brackets - eg:
.........(....<TAG1>...<TAG2>....)......
The following script works, but I've got the feeling that it's not
very efficient. So, can any of you suggest improvements over my
perl-newbie script?
#!/usr/bin/perl
#open output file, tag data, and the form
open (OUTPUT,">OUTPUT.ps") || die "can't open output file";
open (DATA,"DATA.PDF") || die "can't open data file";
open (FORM,"FORM1.ps") || die "can't open form file";
#Read in pipe delimited file (PDF), put data in associative array
@data=<DATA>;
foreach $item (@data) {
@items=split /\|/,$item;
$variable=$items[0];
$value=$items[1];
$$variable=$value; chop $value;
$array{$variable}=$value;
};
#read through form line by line, identifying any tags and replacing
#values from associative array as neccesary
while(<FORM>) {
$stuff = $_;
$done_stuff = "";
while ($stuff =~ /<[0-9a-zA-Z-_]+>/) {
$done_stuff = $done_stuff . $`; #write out pre match
$stuff = $'; #move post match to $stuff to loop again...
$tag_name = $&;
$tag_name =~ s/<|>//g; #take off < and >
if (exists $array{$tag_name}) { #replace if exists in array
$done_stuff = $done_stuff . $array{$tag_name};
} else {
$done_stuff = $done_stuff . "<$tag_name>";
}
}
$done_stuff = $done_stuff . $stuff;
print OUTPUT $done_stuff;
};
close (FORM);
close (DATA);
close (OUTPUT);
Regards,
Ghee