I've always found the "obfuscated xor" technique fun. I avoid it in "real" code 'cause it is very unreadable. But it can be fun to amaze and bewilder your friends and co-workers.
$a=$a ^ $b; $b=$a ^ $b; $a=$a ^ $b; $b=$b ^ $c; $c=$b ^ $c; $b=$b ^ $c;
I end up swapping $a with $b, then $b with $c, (2 2-way swaps) not very original. I'm pretty sure that I (or someone else) could come up with a shorter piece of code that does the same thing, by taking advantage of the fact that it is a 3-way swap.

One cool thing is because of how perl implements xor on scalars is that this works for "strings" in addition to working for "numbers". Still won't work for refrences or undef though :(.

UPDATE

I've done some experiments and managed to find 90 diffrent ways of doing the xor 3-way swap with 6 xor operations. I wasn't able to find any with fewer than 6 xor operations. The only "advantage" is that some of the new ones I discovered are harder to read/follow. Example:

$b=$b^$c; $a=$a^$c; $c=$a^$c; $a=$a^$b; $a=$a^$c; $b=$a^$b;

In reply to Re: interchanging variables the tough way by lhoward
in thread interchanging variables the tough way by ANKUR

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.