in reply to Swapping two values
In addition to the perl idiom of using a list assignment to swap values without a temporary variables, there is the idiom using the XOR operator. Although this does take three statements and doesn't fully take advantage of perl's list assignments, and so is more useful in other languages.
$a ^= $b; $b = $a ^ $b; $a = $a ^ $b;
Update:Here's a contracted form of the above:
$a = $a ^ ($b = ($a ^= $b) ^ $b);
--
integral, resident of freenode's #perl
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Swapping two values (Don't use XOR. Kill this meme.)
by sauoq (Abbot) on Feb 15, 2003 at 10:50 UTC | |
|
Re: Re: Swapping two values
by Anonymous Monk on Feb 15, 2003 at 16:43 UTC | |
by sauoq (Abbot) on Feb 15, 2003 at 21:46 UTC | |
by adrianh (Chancellor) on Feb 15, 2003 at 23:36 UTC | |
by sauoq (Abbot) on Feb 16, 2003 at 00:34 UTC | |
by Anonymous Monk on Feb 16, 2003 at 03:30 UTC |