http://qs1969.pair.com?node_id=45286


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.