[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [Fun With Perl] How to swap $p and $q without $tmp



>From vlb@cfcl.com  Sun Jun 13 01:11:15 1999 - apologies for the delay - vlb]
Message-ID: <19990613080355.123.qmail@plover.com>
In-reply-to: Your message of "Sun, 13 Jun 1999 03:54:25 EDT."
              <19990613035425.A5307@panix.com>
Date: Sun, 13 Jun 1999 04:03:54 -0400


> On Sun, Jun 13, 1999 at 12:28:19AM -0700, Derek Balling wrote:
> > At 02:49 AM 6/13/99 -0400, David H. Adler wrote:
> > >On Sat, Jun 12, 1999 at 01:06:32AM -0400, mjd-list-fwp@plover.com wrote:
> > > > <foghorn>It's a joke, son.</foghorn>
> > >
> > >s/It's/$1, I say, $1/
> >
> > I truly believe you wanted :
> >
> > s/(It's)/\1, I say, \1/
> >
> > There. Didn't you?
> >
> > Or did I miss something in the way regexes work? :)
>
> Nope, I'm just tired.  :-)

They both work.  But some people say it's better to use the $1
variables on the right-hand side of a subtitution for a couple of
reasons:

1. To emphasize the fact that the right-hand part is like a
    double-quoted string and not a like a regex.

2. Because if you need to follow \1 with a numeral, you're out of luck,
    but if you're using the $1 forms you can say ${1}.  Example:

	s/(x*)/\10/;	 # Oops---this always discards the x's
	s/(x*)/${1}0/;	 # This was actually what was wanted.

3. Since the right-hand part of a s/// is like a double-quoted string,
    it's something of a hack that the \1 form works at all.  It's there
    mostly for compatibility with sed.

> > (I'll feel really stupid if I'm wrong, since I sit next to Jeffrey Friedl
> > at work *grin*)

Should have asked Jeff first.

==== Want to unsubscribe from this list? (Don't you love us anymore?)
==== Well, if you insist... Send mail with body "unsubscribe" to
==== fwp-request@technofile.org

</x-flowed>