8 Replies Latest reply on Aug 25, 2006 9:54 AM by Dan McAdam

    Associative Array (Object) problems

    Dan McAdam
      Here is the function i'm dealing with
      i'm reading in a delimited string and using indexed arrays to break them up and assign the keys and values to an associative array in a loop.
      i'm using variables in the loop and the array loads as expected in the loop
      but outside the loop, the only key is the variable name and the value is undefined
      this is true using dot or array notation, as well as literal strings for the keys
      any help is appreciated

      watchSuspendData = function (id, oldval, newval):String {
      //the incoming suspendData string is delimited by the semicolon;
      //newval is: firstValue=Yes;captivateKey=1
      var listSuspendData:Array = newval.split(";"); // convert it to a list of key/value pairs
      if (listSuspendData.length > 0){
      //line 123: listSuspendData.length is: 2
      for (i=0; i < listSuspendData.length; i++){ //for each key/value pair
      var keyValArray:Array = new Array();
      var myNameValue:String = listSuspendData ;
      //line 127: listSuspendData
      is: firstValue=Yes
      keyValArray = myNameValue.split("="); // split 'em on the equal sign
      var myKey:String = keyValArray[0];
      var myVal:String = keyValArray[1];
      //keyValArray[0] is: firstValue
      //keyValArray[1] is: Yes
      // store the key and the value in associative array
      suspendDataArray.myKey = myVal;
      trace("line 134: suspendDataArray is: " + suspendDataArray.myKey);
      // trace is line 134: suspendDataArray is: Yes on the first pass and 1 on the second

      }
      //the below loop always returns one array key: myKey and the value as undefined
      for(x in suspendDataArray){
      trace("x is: " + x); //x is: myKey
      trace("the val is: " + suspendDataArray.x); //the val is: undefined
      } //end for

      }
      return newval;
        • 1. Re: Associative Array (Object) problems
          blemmo Level 1
          The code posted is broken, please use the attach code button for array code like this, because the forum uses [ i ] as italic markup.
          I think the last trace line should be
          trace("the val is: " + suspendDataArray[x])
          with dot notation, 'x' isn't handled as a variable, but literally as the property name. In array syntax, it's used as a variable that contains the name of the property.

          hth,
          blemmo
          • 2. Re: Associative Array (Object) problems
            Dan McAdam Level 1
            thanks blemmo... i reposted the code correctly and added line numbers
            i'll make an additional reply to comment so i can see the line numbers
            just a second
            • 3. Re: Associative Array (Object) problems
              Dan McAdam Level 1
              on lines 12-13 i assign the key=value pair to string variables
              then on lines 17-18 i assign those values to the associative array using dot notation
              the trace seems to work there
              the problem is that when the procedure exits the for loop, the associative array only has one key (myKey) and no value (undefined)
              all the documentation i've read shows using these types of arrays with either non-quoted property names like:
              myAssocArray.myKey = "somevalue";
              or
              myAssocArray[myKey] = "somevalue";

              i tried assigning the key/value pairs directly from the indexed arrays, but the result was always undefined
              like this:
              suspendDataArray.keyValArray[0] = keyValArray[1]
              or
              suspendDataArray[keyValArray[0]] = keyValArray[1]

              i even tried building a string in the loop and trying to assign all the pairs at once using the curly brace
              this is pretty wierd behavior for actionscript or i'm missing something basic here
              thanks for looking


              • 4. Re: Associative Array (Object) problems
                Level 7
                > 8. var keyValArray:Array = new Array();

                remove that line .. it does nothing other than create an empty array every
                loop that is then replaced almsot imediately .. waste of code and time.

                > 11. keyValArray = myNameValue.split("="); // split 'em on the
                > equal sign

                if you want typing, make this

                var keyValArray : Array = myNameValue.split("=");

                > 17. suspendDataArray.myKey = myVal;

                this is your main problem .. it should be

                suspendDataArray[myKey] = myVal;

                --
                Jeckyl


                • 5. Associative Array (Object) problems
                  injpix Level 3
                  And an addition to what others have posted you still need to declare your suspendDataArray variable. You would want to use one of the following examples;

                  var suspendDataArray = {};
                  var suspendDataArray = new Object();

                  Also you would want to have that “return” statement inside the function. I think you might have accidentally forgot a final bracket.
                  • 6. Re: Associative Array (Object) problems
                    Dan McAdam Level 1
                    thanks Jeckyl and myIP...
                    this code is in an actionscript file where i declare a number of variables at runtime including the array using the Object
                    var suspendDataArray:Object = new Object();
                    i have "dumbed down" a lot of this procedure to expose each transaction for debugging
                    but i agree on the keyValArray statement from Jeckyl (thanks!)
                    but, when i use:
                    "this is your main problem .. it should be
                    suspendDataArray[myKey] = myVal;"
                    suspendDataArray is always undefined even in the loop
                    i should be able to directly assign the key value pairs, as i mentioned earlier like this:
                    suspendDataArray.keyValArray[0] = keyValArray[1]
                    or
                    suspendDataArray[keyValArray[0]] = keyValArray[1]
                    but, again... its always undefined
                    thanks again

                    • 7. Re: Associative Array (Object) problems
                      blemmo Level 1
                      You need to use the array syntax for this. Try the attached code, changes are in lines 19-21 and 28.

                      greets,
                      blemmo

                      • 8. Re: Associative Array (Object) problems
                        Dan McAdam Level 1
                        thanks very much to blemmo and all who replied
                        using the correct array syntax solved the problem
                        really appreciate the help!