pilot_vijay: This is an adaptation on the method I proposed last night in the Chatterbox when you asked this question. Also remember the suggestion to read perlop, perlretut and perlre. Therein you will gain a greater understanding of the concepts at work.

The method I proposed last night was to put your RE patterns as values in a hash, "quoted" using the qr// mechanism so that possibly the RE's could be precompiled for efficiency. My earlier suggestion was based on the idea that the keys to the hash would be the replacement value. Iterating over the hash would produce replacement/pattern pairs. But that was based on the idea that you had multiple replacement values. Now that the question is better defined, I see that there is little need for a hash, and an array is a good choice.

That said, here's my recommendation:

# Prebuild the RE patterns for efficiency's sake. my @patterns = ( qr(pattern1), qr(pattern2), qr(pattern3), qr(pattern_n) ); # Use paragraph mode in case your pattern spans multiple lines. # This may need to be combined with the /s switch on your # regexp. Since we don't know what the patterns look like, # I'll use this mechanism to be on the safe side. { local $/ = "\n\n"; while ( my $paragraph = <FILE> ) { $paragraph =~ s/$_/pagingRAC/gs foreach @patterns; print TEMP_OUT $paragraph; } }

My example prints the results of the substitution out to a temporary output file that presumably you open earlier in the script. And again, paragraph mode is used so that patterns that span linebreaks will work ok. But you may not need that part. Also, if your list of RE's is short, you may just join them all together into a big alternation chain. And if the file you're running through is small enough, you may just want to slurp it in, though that doesn't scale well.

Is this question related to the barrage of questions you posted a month or so ago about converting PDF files to HTML, DOC files to HTML, and so on? If so, that might provide additional context that we could relate to in giving helpful answers.

Update: Fixed the foreach. Thanks Roy Johnson for the correction. The foreach was in my head but failed to make it to my fingertips. ;)


Dave


In reply to Re: Substitute array value in regular expression by davido
in thread Substitute array value in regular expression by Anonymous Monk

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.