6 Replies Latest reply on Oct 2, 2007 2:04 PM by Destryp

    complex conditional loop

    Destryp
      In the script I am working on I have a URLVariables instance (wordBank) that consists of 20 name value pairs. The format for each one is as follows:

      word1=<word>
      word2=<word>, etc, through
      word20=<word>

      I want to assign each <word> as the label of a corresponding button. The button instance names are:

      word_btn1
      word_btn2, etc

      When I do one at a time in a line of code like:
      word_btn1.label = wordBank.word1

      it works, but when I try to do it in the loop below, all the buttons are assigned the variable name instead of the value. Is it possible to amend the code to do what I want or will I have to come up with a different strategy?

      Thanks in advance
        • 1. Re: complex conditional loop
          Level 7
          Destryp,

          > Is it possible to amend the code to do what I want or will
          > I have to come up with a different strategy?

          It's possible to amend your code, and the trick is even the usage of the
          array access operator, as you've shown:

          > for (var i = 1; i<20; i++) {
          > ["word_btn" + i].label = ["wordBank.word" + i];
          > }

          But the brackets aren't enough. The brackets allow you to specify a
          property name as a string, and they interpret that string as the property's
          actual name. What's missing above is the object reference to which your
          properties belong.

          If all of these buttons (word_btn1, word_btn2, etc.) are scoped to the
          same object -- let's say the object is referenceable by the "this"
          keyword -- and if workBank is also scoped to the same object, then the
          following would do it:

          for (var:int i = 1; i<21; i++) {
          this.["word_btn" + i].label = this.["wordBank.word" + i];
          }

          See the difference? The bracket's surround properties that belong to
          some object. If "this" isn't the right scope, then you'll have to name the
          object instead. Note, too, that I've typed your i variable as int and set
          the middle condition to i<21, because you said you had 20 words, and you're
          starting with the number 1.


          David Stiller
          Co-author, Foundation Flash CS3 for Designers
          http://tinyurl.com/2k29mj
          "Luck is the residue of good design."


          • 2. Re: complex conditional loop
            clbeech Level 3
            Hey there, here you go this should do it:

            • 3. Re: complex conditional loop
              Level 7
              clbeech,

              > Hey there, here you go this should do it:
              > ...
              > this['word_btn'+i].label = wordbank['word'+i];

              Ah, good! I goofed in my own reply on the wordbank part. You nailed
              it.


              David Stiller
              Adobe Community Expert
              Dev blog, http://www.quip.net/blog/
              "Luck is the residue of good design."


              • 4. Re: complex conditional loop
                clbeech Level 3
                Thanks Dave, weird though, I'd posted this before your response had shown up, sorry about that, why is it that the newsgroup user posts don't show up right away, I often find that I've posted a response to something that someone just answered? I feel like I'm stepping on people toes LOL!
                • 5. Re: complex conditional loop
                  Level 7
                  clbeech,

                  > I'd posted this before your response had shown up, sorry
                  > about that, why is it that the newsgroup user posts don't
                  > show up right away

                  No worries. I suspect it's because of the delay between the Web
                  interface and the NNTP (newsreader) interface. I use a news client in these
                  forums because I prefer that to a browser when answering questions. The
                  interfaces have been slightly out of sync for as long as I can remember.


                  David Stiller
                  Co-author, Foundation Flash CS3 for Designers
                  http://tinyurl.com/2k29mj
                  "Luck is the residue of good design."


                  • 6. Re: complex conditional loop
                    Destryp Level 1
                    Thanks to both of your for the response. I've got it working now, and I also understand the concept a little better from the explanation David posted.