views:

1188

answers:

1

for an XML file, I want to create an array in actionscript where I can reference a particular value with a key I set rather than 0, 1, 2 etc

buildings = myParsedObjectFromXML;

var aBuildings = new Array();

for ( building in buildings ) {
    var currentBuilding = buildings[building][0];
    var key:String = currentBuilding.buildingCode;

    aBuildings[key][property1] = currentBuilding.someOtherValue;
    aBuildings[key][property2] = currentBuilding.aDifferentValue;
    ... etc
}

So that I can access the data at a later date like this:

// building description
trace( aBuildings[BUILDING1][property2] );

but the above isn't working - what am I missing?

+1  A: 

I would start by instantiating my aBuildings variable as an Object rather than an Array:

var aBuildings = new Object();

Next, you need to create an Object first for the key in which you want to store the properties.

aBuildings[key] = new Object();
aBuildings[key]["property1"] = currentBuilding.someOtherValue;
aBuildings[key]["property2"] = currentBuilding.aDifferentValue;

Then you should be able to read the values from the aBuildings object:

trace( aBuildings["BUILDING1"]["property2"] );

Keep in mind that if BUILDING1 and property2 are not String variables you need to use String literals.

Luke
+1. Associative "arrays" serve no useful purpose AFAIK. If you want numeric ordering, use arrays. If you want to access by keys, use object. Also "{}" is a shortcut for "new Object()"
Chetan Sastry
@Chetan Sastry: An Object in Actionscript IS an associative array :)
Luke