Search Unity

How Hashtables work?

Discussion in 'Scripting' started by Alessandro, Jul 4, 2007.

  1. Alessandro

    Alessandro

    Joined:
    Apr 25, 2007
    Posts:
    17
    Hi, need to use an array with properties.
    In Flash I'm used to write something like:

    Code (csharp):
    1. _array = new Array();
    2. _array[0] = {a:"a_value", b:100}
    3. trace(_array[0].a+" - "+_array[0].b)
    If I'm right I've to use an hashtable, so I've tried this:

    Code (csharp):
    1. var _array = new Hash();
    2. _array = {"a" : "a_value", "b": "100"};
    But I get this error:

    Code (csharp):
    1. No appropriate version od 'Boo.Lang.Hash' for the argument list '()' was found.
    I've tried other ways but with no luck. Where is my mistake?

    Thanks
    Alessandro
     
  2. hsparra

    hsparra

    Joined:
    Jul 12, 2005
    Posts:
    750
    I am not sure, but I believe new Hash() creates a .Net Hash object, which is part of System.Security.Policy. I believe what you want is just
    Code (csharp):
    1.  
    2. _array = {"a" : "a_value", "b": "100"};
    3.  
     
  3. Alessandro

    Alessandro

    Joined:
    Apr 25, 2007
    Posts:
    17
    Right! It's so simple! :oops:

    Code (csharp):
    1. _array = new Array();
    2.  
    3. _array[0] = {"a" : "a_value", "b": "100"};
    4. _array[1] = {"a" : "a2_value", "b": "200"};
    5. _array[2] = {"a" : "a3_value", "b": "300"};
    6.    
    7. // Display something
    8. Debug.Log(_array[0]["a"]);
    9. Debug.Log(_array[1]["a"]);
    10. Debug.Log(_array[2]["a"]);
     
  4. Proto

    Proto

    Joined:
    Aug 20, 2007
    Posts:
    79
    I've been exploring JavaScript associative arrays / hash tables. Here's some working examples with notes.

    Code (csharp):
    1. // ** using a {} constructor like Flash
    2. public var connectorObj: Hashtable = {"a": true, "b": false};
    3. for (key in connectorObj.Keys) {
    4.      Debug.Log ("connectorObj:" + connectorObj[key]);
    5. }
    - Members cannot be strict typed
    - Members cannot be exposed to the inspector

    Code (csharp):
    1. // ** extending the Hashtable class
    2. class Connector extends Hashtable {
    3.    var a: boolean;
    4.    var b: boolean;
    5. }
    6. public var connectorClass: Connector = new Connector();
    7. connectorClass.Add("a", true);
    8. connectorClass.Add("b", false);
    9. for (key in connectorClass.Keys) {
    10.      Debug.Log ("connectorClass:" + connectorClass[key]);
    11. }
    - Members can be strict typed
    - Members can be exposed to the inspector


    Code (csharp):
    1. // ** using a Hashtable constructor
    2. public var connectHash: Hashtable = new Hashtable();
    3. connectHash.Add("a", true);
    4. connectHash.Add("b", false);
    5. for (key in connectHash.Keys) {
    6.      Debug.Log ("connectHash:" + connectHash[key]);
    7. }
    - Members cannot be strict typed
    - Members cannot be exposed to the inspector


    It seems to me that using a class gives me the most desirable outcome.
     
  5. raoul

    raoul

    Joined:
    Jul 14, 2007
    Posts:
    6,735
    Thanks for posting this Proto!
     
  6. hai_ok

    hai_ok

    Joined:
    Jun 20, 2007
    Posts:
    193
    I've tried about 8 or 9 different variations on the Hashtable and this (connectorClass) was the first one I got working (for my purposes).

    Thanks Proto! This was a huge help!