Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

substitution op. (s///) won't work

by Deda (Novice)
on Sep 30, 2003 at 08:25 UTC ( [id://295211]=perlquestion: print w/replies, xml ) Need Help??

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

hello, I am trying to replace variables in a string (indicated by /\$\w+/), but am for some reason unsucessfull. This will probbably illustrate the problem best:
DB<7> x $sql 0 'This string represents a SQL statement $replaceme' DB<8> x $what 0 '$replaceme' DB<9> x $with 0 'value' DB<10> $sql=~s/$what/$with/ DB<11> x $sql 0 'This string represents a SQL statement $replaceme'
What i am trying to achieve is
$sql eq 'This string represents a SQL statement value'
Any suggestions what i am doing wrong here?

Replies are listed 'Best First'.
Re: substitution op. (s///) won't work
by Abigail-II (Bishop) on Sep 30, 2003 at 08:45 UTC
    The problem is the content of the variable $what. It contains $replaceme, and for the regular expression engine, this means you want to replace a string 'replaceme' that follows the end of the string, because that's what $ means. Of course, there is no such string.

    You might want to use something like:

    $sql =~ s/\Q$what/$with/;

    Abigail

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-04-19 04:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found