Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: More than one way to do it???

by FouRPlaY (Monk)
on Dec 06, 2000 at 23:24 UTC ( #45286=note: print w/replies, xml ) Need Help??


in reply to More than one way to do it???

If my understanding of merlyn's book and or posts (I can no longer remember which, or even if it was merlyn); you use \1 on the "right hand part" of s///, and $1 on the "left hand part."

Uh, so for getting rid double words you'd use:
s/(\w+)\s+\1/$1/
Or something.

But then again, I could be horribly, horribly, wrong.



FouRPlaY
Learning Perl Or Going To die() Trying

Replies are listed 'Best First'.
Re: Answer: More than one way to do it???
by mirod (Canon) on Dec 06, 2000 at 23:41 UTC

    Actually you have to use \1 in the left part, you can't use $1, but you can use either \1 or $1 in the right part.

    $1... cannot be used in the left part because it would be expanded (the $1 from the previous regexp) and usage of \1 in the right part is mildly frowned upon, mostly for stylistic reasons, as it is close to the \nn or \nnn notation for octal characters (although as usual Perl will DWIM by interpreting \12 as $12 if there were more than 12 captured expressions and as the character \12 otherwise).

      This is correct. According to the Camel book, the right hand side of a substitution (//) is considered to be outside of the regex. So the "special" variable (i.e. \1) is treated as a "normal" scalar variable thus it can be interpolated there because this side functions as if it was a double quoted string.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2022-12-05 07:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?