Array (Object)
Previous  Top  Next

An array is an Object that is composed of a number of items. Each item is contained an element, which is indexed by means of a subscript. The subscript is enclosed within [] and can be a numeric or string constant or a variable.

Sample
dayArray = new Array;
dayArray[0] = "Monday";
dayArray[1] = "Tuesday";
dayArray[2] = "Wednesday";
dayArray[3] = "Thursday";

Arrays are a convenient way of handling tables of data. Access to the specific element is via an integer, which could be another variable. This property makes an array an extremely powerful tool, especially when used with looping constructs such as while, do while etc.

The first 3 days of the week could be conveniently traced with the following code:

while (i < 3) {
   trace(dayArray[i++]);
}

Subscripts can also be strings.
dayArray = new Array;
dayArray["M"] = "Monday";
dayArray["Tu"] = "Tuesday";
dayArray["W"] = "Wednesday";
dayArray["Th"] = "Thursday";

abbrev = "Tu";

dayArray[abbrev] contains "Tuesday"

Creating Arrays
Arrays can be created in any of the following ways:

name = new Array;  
name[index] = "value";  
name = new Array(length);             (Note: won't work with SWF4 Movies)  
name = new Array(value1, value2...);          (Note: won't work with SWF4 Movies)  
name = new Array(name1:value1, name2:value2...);    (Note: won't work with SWF4 Movies)  

For SWF5+ you need to create an array Object and assign it to a variable BEFORE you can start accessing or setting the variable as an array. Therefore simply going name[index] = "value"; won't work. For this reasons the RECOMMENDED WAY to do arrays that are compatible for both SWF4 and SWF5+ is:

name = new Array;  

Flash MX Differences
The Flash MX array Object contains a number of methods that can be applied to it, such as concat, join, etc. The SWiSHscript array Object does not support any Flash MX array Object methods.

This SWiSHscript array Object can be indexed via strings and string constants. Flash MX does not support this feature.