Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

substitution problem with ()

by sroy5 (Initiate)
on Jun 12, 2007 at 16:26 UTC ( [id://620752]=perlquestion: print w/replies, xml ) Need Help??

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?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://620752]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-04-25 07:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found