Retrieving Information from an Array


When retrieving information from an array, use the index of the array element to pull that specific piece of data, as shown here:

 var my_array:Array = new Array("fName","lName","age"); trace(my_array [1]); //output: lName 

In this example, we simply call the second element in my_array, which has the index of 1 because arrays start counting at 0.

There is a way to count the number of elements within an arrayusing the length property. It is the only property that an array has. Just attach the length property to any array with a period, and it will return the length. Here's an example:

 var my_array:Array = new Array("fName","lName","age","location"); trace(my_array.length); //output: 4 

TIP

Remember, the last element in any array will always be the value of array.length minus 1.


When combined with loop statements, the length property can be used to retrieve sequential information.

This example lists each element vertically in the output window, as opposed to all in one line. Place this code in the first frame of the main timeline actions of the movie:

 var my_array:Array = new Array("fName","lName","age","location"); //the loop statement to cycle through the Array for(var i:Number = 0; i < my_array.length; i++){    trace(my_array[i]); } //output: fName //        lName //        age //        location  

This is just a simple example of how to use a loop statement and the length property.




Macromedia Flash Professional 8 Unleashed
Macromedia Flash Professional 8 Unleashed
ISBN: 0672327619
EAN: 2147483647
Year: 2005
Pages: 319

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net