4 Replies Latest reply on May 24, 2006 6:24 AM by Newsgroup_User

    Sorting Variables & adding a number to the name

    JstMrd Level 1
      Hello,

      The Issue:

      Have input boxes on stage. Would like to compare the results to an array through a loop. I am not fully understanding how.

      Taking the existing Input Text Box Variable enter_1 (1- 14) and assigning the number to a standard variable when testing:

      enter_[x]
      enter_(x)
      [enter_+x]

      I have attached the loop code, but may not be on the right track.

      Thank You,
      D
        • 1. Sorting Variables & adding a number to the name
          .:}x-=V!P=-x{:. Level 3
          if (["enter_"+countResponse] == namesArray[countNum]) {
          nameCorrect = (nameCorrect+1);
          ["output_"+nameCorrect] = ["enter_"+countResponse];
          trace(["output_"+countResponse]);
          }
          • 2. Re: Sorting Variables & adding a number to the name
            Level 7
            > ["enter_"+countResponse]

            The example shown here uses the array access operator, []. It seems
            like a neat trick, but once you understand it, it's just another tool in
            your arsenal. When accessing elements in an array, use the array access
            operator as follows:

            myArray[0] // accesses the first element
            myArray[1] // access the second, etc.

            When accessing variables or properties -- in fact, any object at all --
            you may use the array access operator as follows:

            myObj["propertyName"]

            ... which is just a way of specifying this ...

            myObj.propertyName

            ... except you use a string for the property name. In dot syntax, the
            property name (aka variable, or object) is preceeded by a dot; with the
            array access operator, the property name is not preceeded by a dot, but
            encased in square brackets. Content inside the square brackets must resolve
            to a string.

            So in the original post ...

            trace(enter_[countResponse]);

            ... should be ...

            trace(this["enter" + countResponse]);

            ... because countResponse resolves to a number, which, when concatenated
            with a string, ultimately becomes a string. This won't work without the
            "this" (or some other parent object reference), because the array access
            operator accesses the property *of* an object. Without something in front
            of the brackets, ActionScript will think you're trying to create a new
            array.


            David
            stiller (at) quip (dot) net
            Dev essays: http://www.quip.net/blog/
            "Luck is the residue of good design."


            • 3. Re: Sorting Variables & adding a number to the name
              JstMrd Level 1
              Well thanks to both .:}x-=V!P=-x{:. (pronounce??) and David Stiller. You both had some insight and a great explanation from David. I now understand this better. Do you think all these books that I have barricaded myself with could actually get to the point, like this does this.

              Anyways, thanks and here is a summary of what I have gathered from both of you and one other source:

              Answer 1)

              if (["enter_"+countResponse] == namesArray[countNum]) {
              nameCorrect = (nameCorrect+1);
              ["output_"+nameCorrect] = ["enter_"+countResponse];
              trace(["output_"+countResponse]);
              }

              Answer 2)
              trace(this["enter" + countResponse])

              Answer 3)
              eg.
              var reference = eval("enter_" + countResponse);
              trace(reference);

              OR

              var reference = this["enter_"+countResponse];
              trace(reference);
              • 4. Re: Sorting Variables & adding a number to the name
                Level 7
                JstMrd,

                > Anyways, thanks and here is a summary of what I have
                > gathered from both of you and one other source:

                Okay.

                > Answer 1)
                >
                > if (["enter_"+countResponse] == namesArray[countNum]) {
                > nameCorrect = (nameCorrect+1);
                > ["output_"+nameCorrect] = ["enter_"+countResponse];
                > trace(["output_"+countResponse]);
                > }

                In your if() statement, the first part of your condition,
                ["enter_"+countResponse], is shorthand for an array. In affect, those
                brackets, [], are the same as using the constructor new Array(). This isn't
                what you want.

                > Answer 2)
                > trace(this["enter" + countResponse])

                In this useage, those brackets, [], are performing the service of the
                array access operator. That's different. Because of the object immediately
                in front of the brackets (in this case, "this"), the brackets are resolving
                a string into an object property. The word "this" could be _root, _parent,
                or any other term that points to an object (such as an object's instance
                name).

                > Answer 3)
                > eg.
                > var reference = eval("enter_" + countResponse);
                > trace(reference);

                Sure. That would do it, too. eval() is very similar to the use of the
                array access operators in this context.

                > OR
                >
                > var reference = this["enter_"+countResponse];
                > trace(reference);

                Right. Those say the same thing. But you don't even have to use the
                variable.

                trace(eval("enter_" + countResponse));
                trace(["enter_" + countResponse]);

                Those are practically identical.


                David
                stiller (at) quip (dot) net
                Dev essays: http://www.quip.net/blog/
                "Luck is the residue of good design."