http://qs1969.pair.com?node_id=225430

young_david has asked for the wisdom of the Perl Monks concerning the following question:

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