0

In cs4 I am attempting to create an associative array where the values are arrays. These arrays have just two elements and I want to call one of these two elements like this:

var array1:Array = [5, "Example String"]
var array2:Array = [7, "Example String 2"]
var associativeArray:Object = {a1:array1, a2:array2}

trace(associativeArray[a1[0]]) // Print out the value of the first element of the first array. Should print out 5

However this does not print out the first element. Curiously if you omit the "[0]" the program does print the entire array like this: "5, Example String".

How would I go about printing just one element from the array which is inside the associative array.

1 Answer 1

1

The sequence of arguments is wrong in your square bracket access operator [ ]. You need to use the correct notation:

// The whole collection.
trace(associativeArray);

// The collection element, square bracket notation.
// The key MUST be a String.
trace(associativeArray["a1"]);

// The collection element, dot notation.
trace(associativeArray.a1);

// Access the element of collection element.
trace(associativeArray["a1"][0]);
trace(associativeArray.a1[0]);

// WRONG. Access non-existent element of the collection.
trace(associativeArray[a1[0]]);
trace(associativeArray["a1"[0]]);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.