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

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



On Sun, 13 Jun 1999, Bek Oberin wrote:
> Roland Giersig wrote:
> > Easy. Just right to left. Equivalent multiline version:
> >  $q ^= $p;
> >  $p ^= $q;
> >  $q ^= $p;
> 
> I must be dumb.   I still don't get it.  Maybe its just too
> late at night?

Well it's wrong, as was pointed out--the shorter string ends up
padded with nulls after the "swap". But supposing there's no "the
shorter string"--i.e. they are of equal length--then think what
happens when $p has value P and $q has Q.

                 $p       $q
                  P       Q
$q ^= $p:         P       Q^P
$p ^= $q;     P^Q^P       Q^P
$q ^= $p;     P^Q^P       Q^P^P^Q^P

which are:      Q                 P


--
Tushar Samant
~
~
:wq


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