Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: How do I pass $1 to s///?

by Happy-the-monk (Monsignor)
on Jan 06, 2006 at 16:54 UTC ( #521536=note: print w/ replies, xml ) Need Help??


in reply to How do I pass $1 to s///?

backreferences in perlre are used to be written \1, \2 ... tweetiepooh informs me that my perldoc used was outdated... and ikegami again corrects that.

Update: your programme prints "This is fish".
What did you expect it to print? "This is fish 1"?

Further update: see ikegami's solution below.

Third and then fourth update - in italics

Cheers, Sören


Comment on Re: How do I pass $1 to s///?
Re^2: How do I pass $1 to s///?
by Anonymous Monk on Jan 06, 2006 at 17:03 UTC
    > your programme prints "This is fish".
    ...and gives a warning about $1 being uninitialised :(

    > What did you expect it to print? "This is fish 1"?
    Yeah. I've tried "fish \1", "fish \\1", "fish \\\1" as well though with no more luck!
Re^2: How do I pass $1 to s///?
by tweetiepooh (Friar) on Jan 06, 2006 at 17:09 UTC
    \1,\2

    If warnings are on using these lead to
    \1 better written as $1

    There is a section in perlre about it.
Re^2: How do I pass $1 to s///?
by ikegami (Pope) on Jan 06, 2006 at 17:42 UTC

    Still not quite right.

    Backreferences are \1, \2, etc in the LHS (regexp part) of the substitution.
    Backreferences are $1, $2, etc in the RHS (substitituion part) of the substitution.

    Lastest perlre (via [doc://perlre])

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://521536]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2014-07-29 08:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (212 votes), past polls