in reply to Re: interchanging variables the tough way (BENCHMARK)
in thread interchanging variables the tough way
Note that XOR will not work properly in at least three cases: if the variable is a large value, a reference, or is undefined. The other two methods handle all three without a problem. (although -w will complain about the undef). Just something to watch out for.
sub xor { $a = $a ^ $b; $b = $a ^ $b; $a = $a ^ $b; } @foo= qw(bar none at all); $a=999999999999; $b=8888888; print "BEFORE: A=$a B=$b\n"; &xor(); print "AFTER: A=$a B=$b\n\n"; $a=\@foo; $b=8888888; print "BEFORE: A=$a ($a->[0]) B=$b\n"; &xor(); print "AFTER: A=$a ($a->[0]) B=$b\n\n"; $a=undef; $b=8888888; print "BEFORE: A=$a B=$b\n"; &xor(); print "AFTER: A=$a B=$b\n\n"; ## Running the above produces: BEFORE: A=999999999999 B=8888888 AFTER: A=8888888 B=4294967295 BEFORE: A=ARRAY(0x87650a4) (bar) B=8888888 AFTER: A=8888888 () B=141971620 BEFORE: A= B=8888888 AFTER: A=8888888 B=0
|
---|
Replies are listed 'Best First'. | |
---|---|
RE: RE: Re: interchanging variables the tough way (BENCHMARK)
by lhoward (Vicar) on Aug 30, 2000 at 20:51 UTC | |
by tye (Sage) on Aug 30, 2000 at 21:09 UTC |