Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Lost when exchanging values

by Sigmund (Pilgrim)
on May 06, 2002 at 10:26 UTC ( [id://164268]=perlquestion: print w/replies, xml ) Need Help??

Sigmund has asked for the wisdom of the Perl Monks concerning the following question:

Hello, fellow monks,
is there a quick way to exchange the values of two vars without using a third one?

i mean if i got $x=1and $y=2 i wanna make $x=$y and $y=$x so that after that, i got $x=2 and $y=1.
I'm pretty confused.
I just tried $z=$x then $x=$y and then $y=$z and it works, but it's awful...
Thanks in advance.

SiG

perl -le 's ssSss.s sSsSiss.s s$sSss.ss .$s\107ss.print'

Replies are listed 'Best First'.
Re: Lost when exchanging values
by Sidhekin (Priest) on May 06, 2002 at 10:30 UTC

    is there a quick way to exchange the values of two vars without using a third one?

    ($x,$y)=($y,$x);

    Just don't try this with anything but scalars :-)

    The Sidhekin
    print "Just another Perl ${\(trickster and hacker)},"

Quick in execution or typing? (Re: Lost when exchanging values)
by demerphq (Chancellor) on May 06, 2002 at 12:04 UTC
    is there a quick way to exchange the values of two vars without using a third one?

    Just a thought but you need to be a little more specific about your definition of quick. If its "quick to type" then yes as the other respondants said you can do

    ($x,$y)=($y,$x);
    If its "quick to execute" then no there is not, as the following benchmark shows
    use Benchmark 'cmpthese'; use strict; my ($x,$y,$u)=(1,2); sub swapL{ ($x,$y)=($y,$x); } sub swapTt{ my $t=$x; $x=$y; $y=$t; } sub swapTu{ $u=$x; $x=$y; $y=$u; } cmpthese (-10,{list =>\&swapL, mytmp=>\&swapTt, glbtmp=>\&swapTu}); __END__ Benchmark: running glbtmp, list, mytmp, each for at least 10 CPU secon +ds... glbtmp: 11 wallclocks (10.01 usr + 0.00 sys = 10.01 CPU) @ 106353 +0.80/s (n=10651261) list: 13 wallclocks (10.19 usr + 0.00 sys = 10.19 CPU) @ 65633 +1.40/s (n= 6686048) mytmp: 9 wallclocks (10.27 usr + 0.00 sys = 10.27 CPU) @ 90580 +7.23/s (n= 9299017) Rate list mytmp glbtmp list 656331/s -- -28% -38% mytmp 905807/s 38% -- -15% glbtmp 1063531/s 62% 17% --
    A little explaination of this might be useful. When you use the list assignment you are secretly asking perl to create (in this case) two temporary variables (as well as do a bunch of overhead like figuring out how many values it actually has to copy), dynamically, and then to assign those variables to the left side. So while it may look like less code it is in fact much much more. This is nice if you need to swap a couple of variables or reorder a list (check out list slices) in a non-time critical part of your application, but when you need to do the same inside a heavily utilized routine (such as when sorting) then using the less elegant scalar temporary is both faster and has less memory overhead.

    And lets not hear about "premature optimization", there are times when you really really will want the the 40%(!) gain. And there are times when you wont... :-) Knowing the difference is the hard part...

    Yves / DeMerphq
    ---
    Writing a good benchmark isnt as easy as it might look.

Re: Lost when exchanging values
by tachyon (Chancellor) on May 06, 2002 at 11:24 UTC

    Here's somthing you can't do in C (and shouldn't do in perl either!)

    $x = 'x'; $y = 'y'; print "x=$x y=$y\n"; swap($x,$y); print "x=$x y=$y\n"; sub swap { ($_[1], $_[0]) = @_ }

    If you modify the values of @_ in a sub you modify the originals....

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Lost when exchanging values
by Anonymous Monk on May 06, 2002 at 10:38 UTC
Re: Lost when exchanging values
by Zaxo (Archbishop) on May 06, 2002 at 20:53 UTC

    Here's a famous old chestnut:

    $ perl -e'my $c=2; my $d=3; $c^=$d^=$c^=$d; print $c,$d,$/' 32 $
    It's worthwhile to run through the bit twiddling by hand and see how that works.

    After Compline,
    Zaxo

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://164268]
Approved by rob_au
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-09-11 08:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The PerlMonks site front end has:





    Results (13 votes). Check out past polls.

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.