14 Replies Latest reply on Apr 22, 2007 11:58 PM by GHMS

    Swapping Variables

    MurMeeKeer
      Hi all,

      How to swap two variables(without creating one more temporary variable)?
        • 1. Re: Swapping Variables
          Greg Dove Level 4
          well I can't think of a way to do it without a supporting function, but if you need to save the extra line of code ... you could set up a function to do it, passing the variable names as strings:

          var a=20;
          var b=50;

          swapVarValues(this,"a","b");

          trace("a="+a);
          trace("b="+b);

          function swapVarValues(thisObject:Object, varAstring:String,varBstring:String) {
          var tmp = thisObject[varAstring];
          thisObject[varAstring]=thisObject[varBstring];
          thisObject[varBstring]=tmp;
          }
          • 2. Re: Swapping Variables
            MurMeeKeer Level 1
            Thanks. But I was asked in an interview to swap the variables without the use of any additional variables, functions, methods etc.
            • 3. Re: Swapping Variables
              Greg Dove Level 4
              Well maybe someone else knows how to do it, I can't think of a way. Could also be a trick question.
              • 4. Re: Swapping Variables
                Greg Dove Level 4
                Alright, it was a challenge, but this seems to work (maybe there are better ways to do it):

                var a=20;
                var b=50;

                a= [ (b= a),b][1]

                trace("a="+a);
                trace("b="+b);
                • 5. Re: Swapping Variables
                  Level 7
                  >> a= [ (b= a),b][1]

                  Nice.

                  --
                  Dave -
                  Head Developer
                  http://www.blurredistinction.com
                  Adobe Community Expert
                  http://www.adobe.com/communities/experts/


                  • 6. Re: Swapping Variables
                    Greg Dove Level 4
                    Dmennenoh: thanks... I'm not gonna rush out and change all my code with it though. lol.
                    I'm not even sure how or why it works! I was just playing around trying to see if I could do it and stumbled on that one.
                    • 7. Re: Swapping Variables
                      Level 7
                      Nice.

                      It works because of the order of assignments (right to left)

                      var a=20;
                      var b=50;
                      a= [ (b= a),b][1]

                      1. The value of b (50) is placed at position 1 of the array
                      2. b is assigned the value of a (20) and placed at position 0 of the array.
                      3. a is assign the value at position 1 of the array (50)

                      It's a nice way to swap values of any type - if you *really* need to save
                      code space. But while the array is not assigned to a named variable, it
                      still needs to be created on the stack (I assume that's how the engine
                      works).

                      The only true swap algorithm (that I know about) can be used on integer
                      values only, and it uses the bitwise XOR operator (^). Example:

                      var a=20;
                      var b=50;

                      a = a ^ b;
                      b = a ^ b;
                      a = a ^ b;

                      // or on 1 line:

                      a=(a^=b)^(b^=a);

                      trace("a="+a);
                      trace("b="+b);







                      • 8. Re: Swapping Variables
                        Greg Dove Level 4
                        Thanks for that - the XOR integer swap looks interesting.

                        Actually I did know a little about how what I did works (I've observed for example that using a for in loop on an array has a similar effect in terms of going from the top of the array stack to the bottom)

                        I used an anonymous array, because I couldn't use a variable (requirements) and I needed something that would provide temporary storage. I couldn't see how to do that with an anonymous object , only with an array. Yes it takes up space too, but I'm sure the garbage collector will be round to clean up soon.

                        I considered using .pop() but couldn't - no method calls - so I was stuck with using array access operator [1] (which I won't consider to be a method in any way!).

                        • 9. Re: Swapping Variables
                          Level 7

                          Here's an anonymous object version of same:

                          var a=20;
                          var b=50;
                          a = {a:b,b:b=a}.a


                          • 10. Re: Swapping Variables
                            Greg Dove Level 4
                            Clever....similar idea... and I see the order is reversed cf. array.

                            but it is creating named properties which is kind of like creating temporary variables.. I know its kind of moot because the array index elements serve the same purpose in the other version and neither version exists in any sense that matters beyond the execution of the line.. (the fact that they have the same name as the original variables is a good disguise too).

                            I wouldn't have considered it possible if someone hadn't asked the question.

                            All very academic. But fun nonetheless isn't it. And confusing to read if you stumbled across it later on - great obfuscation technique! Oh well... better get back to work...
                            • 11. Re: Swapping Variables
                              Level 7
                              If someone asked me do dream this stuff up while in an interview, I'd wonder
                              if I were smart enough to work there. :)


                              • 12. Re: Swapping Variables
                                MurMeeKeer Level 1
                                All are woderful! Thanks a lot!
                                • 13. Re: Swapping Variables
                                  >Vee< Level 1
                                  looks great. Thank you
                                  • 14. Swapping Variables
                                    GHMS
                                    var a = 20;
                                    var b = 50;
                                    a = a+b;
                                    b = a-b;
                                    a = a-b;
                                    trace("a="+a);
                                    trace("b="+b);