About modifying arrays
You can also control and modify the array using ActionScript. You can move values around an
array, or you can change the size of the array. For example, if you want to exchange data at two
indexes in an array, you can use the following code:
var buildingArr:Array = new Array();
buildingArr[2] = "Accounting";
buildingArr[4] = "Engineering";
trace(buildingArr); // undefined,undefined,Accounting,undefined,Engineering
var temp_item:String = buildingArr[2];
buildingArr[2] = buildingArr[4];
buildingArr[4] = temp_item;
trace(buildingArr); // undefined,undefined,Engineering,undefined,Accounting
You might wonder why you need to create a temporary variable in the previous example. If
you copied the contents of array index 4 into array index 2 and vice versa, the original
contents of array index 2 would be lost. When you copy the value from one of the array
indexes into a temporary variable, you can save the value and safely copy it back later in your
code. For example, if you use the following code instead, you can see that the value of array
index 2 (Accounting) has been lost. Now you have two engineering teams but no accountants.
// wrong way (no temporary variable)
buildingArr[2] = buildingArr[4];
buildingArr[4] = buildingArr[2];
trace(buildingArr); //
undefined,undefined,Engineering,undefined,Engineering
You can find a sample source file, array.fla, in the Samples folder on your hard disk. This
sample illustrates array manipulation using ActionScript. The code in the sample creates an
array and sorts, adds, and removes items of two List components. Find the sample file in the
following directories:
In Windows, browse to boot drive\Program Files\Macromedia\Flash 8\Samples and
Tutorials\Samples\ActionScript\Arrays.
On the Macintosh, browse to Macintosh HD/Applications/Macromedia Flash 8/Samples
and Tutorials/Samples/ActionScript/Arrays.
166
Syntax and Language Fundamentals
Need help?
Do you have a question about the FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH and is the answer not in the manual?