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


In reply to Stumped with $_ being modified by young_david

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.