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

Hi,
The prob is as follows:
$_ = "Child(x,y)"; $To = "Child(x,z)"; I need to substitute the above as s/$_/$To/ print $_;
The Outcome should be as Child(x,z).
Note: the $_ data x,y and z are variable numeric depends on the numeric data it gets at run time.

Replies are listed 'Best First'.
Re: substitution problem with ()
by ikegami (Patriarch) on Jun 12, 2007 at 16:39 UTC

    The first argument of the substitution operator (s///) is a regexp, not arbitrary text. An easy way to convert arbitrary text into a regular expressions in to use quotemeta.

    my $from_re = quotemeta($_); s/$from_re/$to/;
      Or, in one go (and IMO simpler):
      s/\Q$from_re/$to/;

      "\Q$string" is the same as quotemeta($string), both in a doublequotish string and in a regexp.

Re: substitution problem with ()
by shmem (Chancellor) on Jun 12, 2007 at 17:01 UTC
    Note: the $_ data x,y and z are variable numeric depends on the numeric data it gets at run time.

    Unclear spec. In you code z isn't contained in $_. Where does it come from? I know what I mean. Why don't you? Read perlretut and perlre, that should get you on the way.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: substitution problem with ()
by thezip (Vicar) on Jun 12, 2007 at 16:46 UTC

    sroy5, could you please provide a few real-world examples of the transformation you seek? There's really not enough context here to see what you're trying to do.


    Where do you want *them* to go today?