Arrays

I l @ ve RuBoard

The last major aspect of programming that we need to cover is arrays. You'll find arrays in almost every programming language; they are almost indispensable tools for complex programming.

graphics/newterm.gif

An array is a list of data. Usually, the data is all of the same type, such as a list of names or a list of movie clip positions .

Making Arrays

Here is an example of an array. You need to use square brackets and commas to build one from scratch:

 myArray = [36,23,63,71,25]; 

This array has five elements, all integers. To get an element of the array, use code like this:

 trace(myArray[0]); 

The first item in an array is always item number 0. So this example array has five items in it, item 0 being the number 36 and item 4 being the number 25.

Another way to create a new array is with the new Array() construct. You can use this to make a global variable or a local variable when you start the line off with var .

 myArray = new Array(); 

When you want to add an item to the end of an array, use the push command. Here is a sequence of lines that create a new array with the same contents as the one before:

 myArray = new Array(); myArray.push(36); myArray.push(23); myArray.push(63); myArray.push(71); myArray.push(25); 

Manipulating Arrays

You can do a number of things with the array after you have it. To get the length of the array, use the length property of the array variable. For instance, this tells you that the array has five items in it:

 myArray = [36,23,63,71,25]; trace(myArray.length); 

You can get the last item in the array, and remove it from the array at the same time, with the pop command. Here is an example:

 myArray = [36,23,63,71,25]; trace(myArray); a = myArray.pop(); trace(a); trace(myArray); 

You first get the starting list of items, showing that there are five items in the array. Then you get a 25 as the value of a . Finally, you get the new list of items, showing you that the array now has only four items, because pop removed the last one.

Using push and pop creates a last-in-first-out system, sometimes called a stack. Think of a stack of pancakes. As the cook makes the pancakes, they pile higher. The top pancake is the one made most recently. When someone takes a pancake, he takes from the top. The last pancake to be eaten is the first one made.

The opposite of pop is shift . This removes the first element from the array. So the following code performs the same as the previous code, but the number 36 is put into a instead:

 myArray = [36,23,63,71,25]; trace(myArray); a = myArray.pop(); trace(a); trace(myArray); 

The opposite of pop is unshift . This puts a new item at the beginning of the array.

To get a portion of an array, use the slice command. The arguments are the first and last items to be taken:

 myArray = [36,23,63,71,25] trace(myArray.slice(1,3)); 

This returns 23,63 because the third item is actually not included. If you omit the second argument altogether, you will get all the items until the end of the array.

The Swiss Army knife of array commands is splice . The first parameter of splice is the position of the array where the operation should begin. The second parameter is the number of items to delete. This can be 0 if you don't want to delete any items. The rest of the parameters are items to insert. You cannot list any items if there are none to insert.

Here is an example where the 23 and 63 are removed, and a 17 is inserted in their place:

 myArray = [36,23,63,71,25]; myArray.splice(1,2,17); trace(myArray); 

Sorting Arrays

You can also sort an array with the sort command. The following code returns the array in numerical order:

 myArray = [36,23,63,71,25]; myArray.sort(); trace(myArray); 

The sort command also sorts a list of strings into alphabetical order:

 myArray = ["Gary","Will","Jay","Brian"]; myArray.sort(); trace(myArray); 
graphics/bulb.gif

Looking to sort an array in some other way besides numerical or alphabetical? You can modify the sort command to do almost anything you want. Such an advanced topic is beyond this book, but if you really need it, you can refer to the documentation for sort to see how to make a specialized sort function.


The reverse command takes an array and reverses the order of the items in it. For instance:

 myArray = ["Gary","Will","Jay","Brian"]; myArray.reverse(); trace(myArray); 

Want to sort an array in descending order? Just use a sort command and then a reverse command to get the job done.

You can combine two arrays with the concat function. This works a little differently because it does not alter either array. Instead, it creates a new one:

 myArray = [36,23,63,71,25] otherArray = [58,97,16]; newArray = myArray.concat(otherArray); trace(newArray); 

Converting Between Strings and Arrays

If you want to convert an array to a string, use the join command. It takes one argument, which would be the character to place between the items. If you don't supply it, it will use a comma. This returns the string 36:23:63:71:25.

 myArray = [36,23,63,71,25] myString = myArray.join(":"); trace(myString); 

Whereas the join command is marginally useful, the split command is very valuable . It converts a string to an array. For instance, this takes the string "36,23,63,71,25" and converts it to the example array we have been using:

 myString = "36,23,63,71,25"; myArray = myString.split(","); trace(myArray); 

Suppose that you have a sentence , and you want to break up each word into the items of an array. Here's how you do it:

 myString = "This is a test"; myArray = myString.split(" "); trace(myArray); 

Now let's look at some examples of arrays in movies.

I l @ ve RuBoard


Sams Teach Yourself Flash MX ActionScript in 24 Hours
Sams Teach Yourself Flash MX ActionScript in 24 Hours
ISBN: 0672323850
EAN: 2147483647
Year: 2002
Pages: 272

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