7 Replies Latest reply on Mar 21, 2008 3:13 AM by Neat Gadget

    Associative Array Syntax

    Neat Gadget
      I want to create an associative array, but using variables instead of hard coding.

      example

      instead of

      assoc['field1']=data;

      I want

      assoc[fieldname]=data;

      I've been reading for ages and cannot seem to find anything. I've tried the above and it doesn't work.

      Any suggestions?
        • 1. Re: Associative Array Syntax
          robdillon Most Valuable Participant
          I tried this:

          var myVar1:String = "foo";
          var thisOne:Object;
          var myVar2:String = "bar";
          var array1:Array = new Array(thisOne,myVar1);
          array1[myVar1] = myVar2;
          trace(array1[myVar1]);

          -- bar

          Is this anything like what you're attempting?
          • 2. Re: Associative Array Syntax
            Neat Gadget Level 1
            Yes I think so, let me give it a go in the morning and get back to you. Thanks for your help.
            • 3. Re: Associative Array Syntax
              Level 7
              Hello Neat,

              > I want to create an associative array, but using variables instead of
              > hard coding.
              >
              > example
              >
              > instead of
              >
              > assoc['field1']=data;
              >
              > I want
              >
              > assoc[fieldname]=data;
              >
              > I've been reading for ages and cannot seem to find anything. I've
              > tried the above and it doesn't work.
              >
              > Any suggestions?


              Don't quite understand you, folks...
              This question is perfectly covered in flash help.
              http://livedocs.adobe.com/flash/9.0/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=Liv eDocs_Parts&file=00000723.html

              Just use either object or array constructor (depends on how will you use
              this array further)


              • 4. Re: Associative Array Syntax
                Neat Gadget Level 1
                Horus,

                I had read that information, it was the first thing I found when I did a search on "creating associative arrays actionscript". The problem is with my understanding of associative arrays being new with respect to Flash. So when I read that page it didn't answer my question, and reading it again now still doesn't seem to tell me anything new. From my understanding of that page, it only talks about hard coded associative array. But I could be reading it wrong.

                However the way Rob has explained it seems to make perfect sense. I'm still trying to get it to work.
                • 5. Re: Associative Array Syntax
                  Level 7
                  Hello Neat,

                  > Horus,
                  >
                  > I had read that information, it was the first thing I found when I
                  > did a search on "creating associative arrays actionscript". The
                  > problem is with my understanding of associative arrays being new with
                  > respect to Flash. So when I read that page it didn't answer my
                  > question, and reading it again now still doesn't seem to tell me
                  > anything new. From my understanding of that page, it only talks about
                  > hard coded associative array. But I could be reading it wrong.
                  >
                  > However the way Rob has explained it seems to make perfect sense. I'm
                  > still trying to get it to work.


                  Maybe the problem is in my misunderstanding of your idea.
                  I just don't quite get your reasons to use associative array with using variables
                  instead of hard code )


                  • 6. Re: Associative Array Syntax
                    Neat Gadget Level 1
                    Ok, I got Robs code to work within the code I am writing. However the problem is now something totally different. And this leads to answering your question Horus.

                    The big picture is that I have a flash exe that has multiple datagrids. What I want to do is create a generic function that I can pass an sql and field list to, and have it populate the datagrid.

                    The function loops through each line of data, and for each line, loops through each column. The idea is to create the associative array to be used by the push method. However this is where I am coming unstuck and perhaps I am approaching this in the wrong way. I used MDM Zinc for MySQL and other calls.

                    • 7. Re: Associative Array Syntax
                      Neat Gadget Level 1
                      Ok I think I have managed to solve it. This seems to work although I am not sure if perhaps I have not initialised things correctly. I had a number of things wrong with the previous code. One was having the push in the cols loop and the other was changing the way I setup the associative array.