8 Replies Latest reply on Jan 31, 2008 5:27 AM by Greg Dove

    Dynamic array naming

    Bob Pierce
      Here's a simple requirement...

      var arrayName:String = "tabAy_"+commTrayNo;
      var this[arrayName] = new Array();

      ..but the second line gives an error: Identifier expected. How can I generate a valid identifier for my new array?
        • 1. Re: Dynamic array naming
          Level 7
          Why is this a requirement? I don't know how you can do this, since I don't
          think eval() will work, and I really can't think of a reason you'd need
          this. What is it you're attempting to do?

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


          • 2. Re: Dynamic array naming
            Bob Pierce Level 1
            Suppose I'm building a menu. I have a series of arrays, each one associated with a tab in the menu. The arrays hold, say, submenu item names, page numbers etc. I'd like to name the arrays after their menu tab number eg tabAy_1, tabAy_2 etc. This way I can reference the array by tab number eg subName = this["tabAy_"+tabNo][subIndx]. The number of tabs varies and is determined at run time from an XML file. Seems like a reasonable requirement to me!

            That's my particular scenario, but dynamically naming variables is common enough. Anyway, I've solved the problem. Using "var" in this situation is inappropriate, the following works:

            var arrayName:String = "tabAy_"+commTrayNo;
            this[arrayName] = new Array();

            as does:

            this["tabAy_"+commTrayNo] = new Array();

            • 3. Re: Dynamic array naming
              Level 7
              I wouldn't call it common and I certainly wouldn't call it good coding
              practice. I've never needed it, and I'd bet money there's not a situation
              where it is necessary. In your case, why not just use an object and stick
              arrays in object properties as needed. Or use another array as a master
              container and reference by index. Sorry, but they way you're doing it seems
              really kludgy to me.



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


              • 4. Re: Dynamic array naming
                Bob Pierce Level 1
                You're probably right! I did plan to use a master array instead if I couldn't get this naming thing sorted. No doubt a few more years of experience will sort me out!
                • 5. Re: Dynamic array naming
                  Level 7
                  I think a plain object would work nicely, as you could still use the names
                  you want. I just find dynamic variable creation bad practice since you're
                  not aware of the vars before the program is executing. Makes watching
                  things, debugging, etc. more trouble.

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


                  • 6. Dynamic array naming
                    Greg Dove Level 4
                    'this' also refers to an object. In this case its likely (if its a plain frame script) that it's referring to a movieclip instance. So you're actually trying to set a property rather than declare a variable. In the end the result is the same because a newly declared variable appears as a property of the movieclip representing its scope.

                    So the problem is with the 'var' declaration, and this will work:
                    commTrayNo=1;//for test purposes

                    var arrayName:String = "tabAy_"+commTrayNo;
                    this[arrayName] = new Array("testElement1","testElement2");

                    trace(tabAy_1); //traces testElement1,testElement2


                    But I'd certainly be more inclined to scope anything like that within a separate object as Dave suggested, or to do it using class definitions (e.g. have a static class which is used to add MenuTabs to and access the MenuTabs collection)
                    • 7. Dynamic array naming
                      Bob Pierce Level 1
                      Thanks for the advice. I shall re-write using object properties as you both suggest.

                      However(!), in accessing the data via these properties I'll still be using a tab index or subMenu index in just the same way as I'd be doing with my dynamically named arrays. The difference doesn't seem to be very significant so I'm not clear why my arrays provoked such criticism. On the other hand I think the level of this discussion has risen slightly above my head!
                      • 8. Re: Dynamic array naming
                        Greg Dove Level 4
                        Oh, its not criticism on my part. I used to do things the way that you describe... I guess if you want to take some advice its that I'm pleased I changed!
                        In the end for me it's mainly preference, partly for the reasons Dave outlined. I like to 'contain' different aspects of whatever I work on. Two reasons mainly:

                        a. Like Dave said its easier to debug if things are isolated... as well as being more object-oriented in the way its built. Being object-oriented is an advantage by nature in that you get the added benefit of isolating 'chunks' of what you do that you could use in other applications if it has broader usefulness.
                        b. Perhaps more importantly and largely related to (a) it helps me stay sane (that's a subjective viewpoint I guess) because I can think about what I'm doing in a more abstract way. In your example (as an example) I would build the MenuTabs as a class (probably two classes - one to manage them and one for instances of the tabs) and do unit testing on it/them. After that I wouldn't think about it/them as code so much anymore... just how I interact with it/them.

                        For me I didn't always code that way and it took me a while to get to 'that stage'. And I still frequently code stuff on the timeline often and then migrate it to classes when it starts to get too big to think about in its totality. But overall it makes things easier in the long run... at least it does for me!