Dear Monks,
I'm looking for advice on how to complete what I'm finding to be a difficult task. What I'm dealing with are two files, (1) tagged ascii file (2) style sheet. Both files are generated from a proprietary typesetting system.
I want to be able to loop through the tagged ascii file and when I match a tag, pass it a subroutine and then replace it with the attributes for that particular tag from the style sheet. How can this be accomplished? Below is a small sample of the script I'm working with.
Thanks,
young_david
use strict;
sub tagheader {
my $header;
open(STYLESHEET,'stylesheet.txt');
my $tag = shift;
###########################################################
# "HEADER" appears before "nm" in the style sheet
# Find each "HEADER" in the file and assign $1 to $header
# If we find the $tag we're looking for return $header
#
while (<STYLESHEET>) {
if (/^HEADER (\d+):/) {
$header = $1;
}
if (/^nm : ($tag)/) {
return \$header;
}
}
close(STYLESHEET);
}
my $body;
my $tag;
open(FILE,'taggedfile.txt');
open(OUT,">$ARGV[0]");
while(<FILE>) {
if (/^\{(\w+)\}/) {
$tag = $1;
$body = tagheader($tag);
# Right here, $_ contains "nm : $tag" from the tagheader()
# I see that $_ has been modified by tagheader()
# This is my problem and I'm stumped :(
s/\{($tag)\}/\{$1:$body\}/;
# I want to search and replace the ($tag) with $1:$body
# $body will contain a number that is assigned to the $tag
# in the style sheet.
}
print OUT $_;
}
close(FILE);
close(OUT);
__DATA__
FILE { type : if }
FILEHDR {
File_Comment : ""
}
HEADER 1: {
_std_comment : ""
nm : TAG
post : ""
ppriority : 1st
pmaxwhite : 0
}
BODY 1:1 {
endval : page
priority : 1st
ffamily : 2
fvariant : 3
fheight : 10q
fwidth : same
maxwhite : 0
prelead : no
ldasc : normal
lindent : 0
pgindent : 0
lnmeas : full
ldextra : 2q
lddesc : normal
qdtbl : l
hjtbl : 4
pgsplit : no
lpabove : 0
lpbelow : 0
laabove : 0
labelow : 0
language : 1
xpath : ""
capmode : no
kernnum : 1
spotcolor : normal
psrnum : 1
ipcrnum : active
pair_kern : yes
endstring : ""
pre : ""
repeat : repeat
pgrful : none
httbl : none
pattern : none
comment1 : ""
}
Edit by tye to add READMORE
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|