[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



Roland wrote:
> 
>  $q ^= $p;
>  $p ^= $q;
>  $q ^= $p;
> 
> BTW, why does this work with strings of different length?
> Deep Perl magic?

If you inspect the variables after each step, you'll discover that
the shorter one is padded with null characters *on the left* before
the first XOR.  This has the effect, after the last XOR, of the other
variable, the one that had been longer, being padded will nulls on
the right, which Perl then (apparently) discards.  Not terribly magic;
more like "automagic".

John Porter


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