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

Hi I am doing a dynamic substituion.ie matching pattern and replacement pattern are written in a file.In the replacement pattern there is a \| and $1.
$x =~ s/(\|[A-Za-z\^\~]+\|)$temp[1]\|/\|$temp[0]\|/;
where $temp[1] is something like "ZO" and $temp[0] is something lik ZEE\|$1\|ZAA. But it is substituitng with $1 instead of the value of $1.How will correct this problem. -manju

Edit Masem 2001-08-23 - Added CODE tags; also changed from a repeat $temp[1] after sub line to $temp[0] based on context.

Replies are listed 'Best First'.
Re: Pattern Matching
by Monky Python (Scribe) on Aug 23, 2001 at 18:18 UTC
    you must evaluate by using the /e option for your regexp
    $temp0="ZO"; $temp1='$1'; $x="ABCZO"; $x =~ s/([A-Za-z]+)$temp0/$temp1/ee; print $x; # should print ABC

    hope this helps

    MP

      Your suggestion works ... but the question was a bit more complicated because $temp = 'foo$1bar'. And then the simple evaluation doesn't work ... you have to employ a little extra trick (proposed by japhy a couple of days ago):

      $temp0="ZO"; $temp1='foo$1bar'; $x="ABCZO"; $x =~ s/([A-Za-z]+)$temp0/qq[qq[$temp1]]/ee; print $x; # prints fooABCbar

      -- Hofmator