1 Reply Latest reply on Dec 15, 2006 12:27 AM by Peter Lorent

    What you think about this Object Array Access Class

    Born2code Level 1
      This is kind of my first script in Actionscript and wanted to get some input.


      /*

      By Sean Levy Dec 14, 2006

      Description:

      This is a Dataset object.

      You pass to it an obecjt which holds an array of data which is constructed of:

      myData[0] = "prim_key value, field1 value, field2 value, field3 value ... etc
      .
      You can access any field of any primey key (i.e.: HH101) directly
      WITHOUT doing a for loop, saving time and resources !!!
      The for loop only happend doing init of the object but not every time
      you read or write to the object making it super fast ...

      The best way to think of this object is as of its a virtual grid
      where you can access any recoed using a row and column identifier ( i.e.: part_number / field )
      where the part number is always a unique prim. key

      The "Magic" of the code is that we use two objects with mapped array indexes
      so we can always retrieve the primary's key index and thus pull the proper
      hash table equivelant member

      Enjoy.


      =========================================================================
      Sample to copy and paste below:
      =========================================================================


      var myData:Array = new Array();

      myData[0] = "part_number HH101,quantity 11,stock 20,description cool stuff";
      myData[1] = "part_number HH102,quantity 12,stock 200,description hello world";
      myData[2] = "part_number HH103,quantity 13,stock 20,description a realy good tail motor for Piccolo (Qty x 2)";

      // Create Dataset Object and populate with myData

      var myData:Dataset = new Dataset(myData);

      var part:String = "HH102";

      trace("Showing part: " + part);

      trace(part + " Stock " + myData.getValue(part, "stock"));

      trace("Changing inv " + part + " to 150"); myData.setValue(part, "stock", 150);

      trace(part + " Stock: " + myData.getValue(part, "stock"));

      trace(part + " Description " + myData.getValue(part, "description"));


      */



      class lib.Dataset {


      private var _aKeys:Array = new Array();
      private var _aKeysExtended:Array = new Array();
      private var _index:Number = null;

      // Get Value from dataset

      public function getValue( partNumber:String, column:String ) {

      _index = _aKeys[partNumber];

      return _aKeysExtended[_index][column]

      }


      // Set Value at dataset

      public function setValue( partNumber:String, column:String, value:String ) {

      _index = _aKeys[partNumber];

      _aKeysExtended[_index][column] = value;

      }

      public function showAll() {


      var sElement:String = null;

      for(var z:Number = 0; z < _aKeysExtended.length; z++) {

      sElement = "";

      for(var key in _aKeysExtended[z]) {

      sElement += _aKeysExtended[z][key] + " ";

      }

      trace(sElement);

      }

      // _aKeysExtended.sortOn("description");


      }


      // Constructor

      public function Dataset( myData:Array ) {

      for ( var i:Number = 0 ; i < myData.length ; i++ ) {

      // trace(i + " " + myData );

      var sLine:String = myData
      ;

      var aSingleLine:Array = sLine.split(",")

      var nCounter:Number = 0;

      var sPrimKey:String = ""


      for ( var x:Number = 0 ; x < aSingleLine.length ; x++ ) {


      // trace("A" + " " + i + " " + aSingleLine[x]);

      var theData:Array = aSingleLine[x].split(" ")

      var theColumn:String = theData[0];

      // trace("The Column: " + " " + theColumn);

      var columLength:String = ( theColumn.length + 1 );

      var lineLength:String = ( aSingleLine[x].length );

      // trace("C" + columLength + " " + lineLength );

      var dataOnly:String = aSingleLine[x].substr(columLength, lineLength);

      // trace(nCounter + " " + theColumn + "=" + dataOnly);

      if ( nCounter == "0" ) {

      _aKeys[dataOnly] = i;

      _aKeysExtended.push({key: dataOnly});

      } else {

      _aKeysExtended [theColumn] = dataOnly

      }

      nCounter++;

      }
      }
      }
      }