Array (Object)
Top  Previous  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)  

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 SWiShscript array object only supposed the MX array object methods when you specify SWF5 or higher. For SWF4, no array methods or properties are supported. Also the array constants and and special constructors are only supported for SWF5 or higher. In addition, using the 'new Array' as shown in the above example is not strictly required for SWF4.