[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



At 04:03 AM 6/13/99 -0400, mjd-perl-@plover.com wrote:

> > 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.

Yup, I was thinking LHS of the s///.... (although as you said, I think the 
\1 will WORK on the RHS, but its certainly frowned upon).

Time for bed, methinks. :)

D

==== 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>