dcsimg
A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: How to push a dynamic number of elements onto an associative array?

  1. #1
    Junior Member
    Join Date
    Mar 2007
    Posts
    21

    How to push a dynamic number of elements onto an associative array?

    Hello,

    I have been working with flash remoting in actionscript 3.0 and there is no result set class. I'm fairly new to AS3, but I am very fluent with AS2, and so far the migration has been easy.

    I would like to create a result set class myself, but I am stuck on one part, pushing the dynamic number of elements onto the array, let me show you what I mean:

    { // start result function
    var totalCount:Number = rs.serverInfo.totalCount; // # of records returned
    var queryString:String = rs.serverInfo.intialData; // query string, each value separated by a comma
    var queryArray:Array = new Array();
    queryArray = queryString.split(",");
    var columnString:String = rs.serverInfo.columnNames; // names of the columns, separated by a comma
    var columnArray:Array = new Array();
    columnArray = columnString.split(",");
    var columnLength:Number = columnArray.length;

    var myResultSet:Array = new Array();
    // now I have the column names stored in columnArray, and the records stored in queryArray, I would like to make myResultSet an associative array with the column names matching the records. The records come back in the same order the column names do, so keep a recordPosition variable like this:

    var recordPosition:Number = 0;
    var columnPos:Number = 0;
    for(var i:int = 0; i < columnLength; i ++){
    recordPosition = i * columnLength;
    // this is the part i'm stuck on, i'm currently hard coding my parsing of the query object now because of this, also I don't know if this is how you eval() correctly in AS 3.0
    queryArray.push({[columnArray[columnPos]]: queryArray[i], ... [columnArray[columnPos+columnLength-1]]: queryArray[i+columnLength-1]);
    }
    }
    From all the examples I've seen you have to push all the elements into the array at once, statically. I think it may just be some kind of concept I am missing, because I want to loop inside of the push(), but I know that is not possible.

    Can anyone enlighten me on this?

  2. #2
    Junior Member
    Join Date
    May 2009
    Posts
    6

    How to push a dynamic number of elements onto an associative array?

    Does anyone know 'How to push a dynamic number of elements onto an associative array?'

    // In ActionScript 3.0

    var aDP_array:Array = new Array;

    //This works:
    aDP_array.push({firstname:"aaa", lastname:"bbb"});


    //Following does not work:
    strData = '{firstname:"aaa", lastname:"bbb"}';
    aDP_array.push(strData);


    I would appreciate any help.

    v/r

    Shahram

  3. #3
    a.k.a gltovar deadlock32's Avatar
    Join Date
    May 2001
    Location
    Naperville,IL
    Posts
    489
    Quote Originally Posted by sghara View Post
    Does anyone know 'How to push a dynamic number of elements onto an associative array?'

    // In ActionScript 3.0

    var aDP_array:Array = new Array;

    //This works:
    aDP_array.push({firstname:"aaa", lastname:"bbb"});


    //Following does not work:
    strData = '{firstname:"aaa", lastname:"bbb"}';
    aDP_array.push(strData);


    I would appreciate any help.

    v/r

    Shahram
    there are a few things to question about your code first,

    strData is not defined in you snipped, it need to be:
    var strData =
    before you use it.

    second your starData value is wrapped in single quotes.

    PHP Code:
    var strData '{firstname:"aaa", lastname:"bbb"}'// this is a String
    // is very different from

    var strData = {firstname:"aaa"lastname:"bbb"}; // this is an Object
    push(strData); 

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Although Arrays are dynamic and you can use them this way, both Object and Dictionary are more natural choices.

    I'm posting from my phone, so I'll update with code in a bit. Check the livedocs for Dictionary for a head-start.

  5. #5
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Code:
    function getResults(rs:Object):Dictionary{ // start result function
      var queryString:String = rs.serverInfo.intialData; // query string, each value separated by a comma
      var queryArray:Array = queryString.split(",");
      var columnString:String = rs.serverInfo.columnNames; // names of the columns, separated by a comma
      var columnArray:Array = columnString.split(",");
      var columnLength:Number = columnArray.length;
    
      var myResultSet:Dictionary = new Dictionary();
      // now I have the column names stored in columnArray, and the records stored in queryArray, I would like to make myResultSet a map between column names and records
    
      for(var i:int = 0; i < columnLength; i ++){
        myResultSet[columnArray[i]] = queryArray[i];
      }
      return myResultSet;
    }
    Usage:
    Code:
    var results:Dictionary = getResults(resultset);
    //assume 'foo' is a column with value 'bar'
    trace(results['foo']); //traces bar

  6. #6
    Junior Member
    Join Date
    May 2009
    Posts
    6
    Thanks. it worked like a charm!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center