In computer programming, there are actually two kinds of arrays, indexed arrays and associative arrays (also known as hashes). In indexed arrays, each array element has an index, whereas in associative arrays, each array element has a name. In ActionScript, the Array class produces indexed arrays, while the Object class produces associative arrays.

In the chapter about control statements, we've covered the use of Array class's constructor function without parameters, and we won't go through it again in this chapter. In this chapter, we will go through two more ways to use the constructor function, as well as the array literal. (Yes, arrays have literals too!)

In the first example, we initialised the dogs array by specifying its length, then assigned a string value to each of the elements. In the second example, we put in the values directly into the constructor function.[1] In the third example, we used the array literal. The array literal is always surrounded by a pair of square brackets, and each element is separated by a comma, like this: [1,1,3,5,8,13,21,34].

Firstly, we declare the 'myArray' array with four elements: 1, 2, 3 and 4. Next, we declare the 'popped' variable. We then pop 'myArray' and assign the last element to the 'popped' variable. We then trace popped to see the element which was popped, then trace the new array.

push() adds one or more elements to the end of the array, and then returns the length of the new array:

We first declare the 'myArray' array with two elements, 1 and 2. The second line pushes the values 3 and 4 into myArray (returning the new length), declares the 'newLength' variable, then assigns the new length to newLength.

Firstly, we declare the 'myArray' array with four elements: 1, 2, 3 and 4. Next, we declare the 'shifted' variable. We then shift 'myArray' and assign the first element to the 'shifted' variable. We then trace shifted to see the element which was shifted, then trace the new array.

We first declare the 'myArray' array with two elements, 3 and 4. The second line unshifts the values 1 and 2 into myArray (returning the new length), declares the 'newLength' variable, then assigns the new length to newLength.

Splicing an array is a more flexible and sophisticated process then pushing, popping, shifting and unshifting. (Slice, split, splice, shift... confused yet?) Splicing is the process of both removing and adding elements to an array. The splice() methods adds elements to and remove elements from the array, then returns the removed elements.

Let's look at the parameters again:

array.splice(index of the first element, [number of elements], [new element 1, new element 2...])

As you can see, only the first parameter is required. The first parameter indicates where you want to start doing your removing and adding work. It can be a normal index (0, 1, 2, 3...), or a negative one (-1 as the last element, -2, -3, -4...). If this is the only parameter you include, all the elements starting from that one will be removed:

Code

Result

var myArray:Array = [1, 2, 3, 4];
myArray.splice(2);
trace(myArray);

1,2

The second parameter is the number of elements to be removed. Let's alter the above example to remove only the 3:

Again, the whole array is copied, not linked. In the above example, changing secondArray does not change the original myArray as myArray was copied to secondArray. However, changing thirdArray does change the original myArray as thirdArray only contains a link to myArray, not a copy of myArray.

Sometimes, we want to split a string into many substrings separated by a delimiter (a symbol showing where we should split the string), then tuck each substring into an array. In this case, we need the split method:

In the first trace, we used " + " as a delimiter to create an array containing the elements Foo, Bar, Baz and Qux. In the second trace, we limited the number of substrings to 3. In the third, the delimiter was a blank string, so the each substring consisted of one character.

Joining an array is the reverse operation of splitting a string. It creates a new string using array elements and a certain delimiter (the first and only parameter):

In the first trace, we joined the Foo, Bar, Baz and Qux into a string separated by ' + '. In the second, we got the word 'Foobar' as we used an empty string as our delimiter. In the third, we produced the same effect as trace(String(["Foo", "Bar", "Baz", "Qux"].join())); since we did not specify any delimiter and the default comma was used.

Note that Lucky and Max come before rover and spot because uppercase letters precede lowercase letters in Unicode. Also note that the numbers are also arranged according to their Unicode code points, not their magnitudes or values.

If we want the sort to ignore casing, we can pass an Array class constant, Array.CASEINSENSITIVE, into the sort() function:

Sometimes, using Unicode code points just isn't enough. In this case, we have to make our own functions. The first parameter of the sort() function is a function with two parameters which decide which of two values should come first. The function should return 1 if the first argument comes before the second argument, -1 if the second argument comes before the first, and 0 if it doesn't matter. If that sounds complicated, look at this example:

a canine called lucky,an adorable animal called Max,a cute mammal called Rover,a good dog called spot

In the whichComesFirst function, we first extracted the last word from the string. Then, we converted everything to uppercase before comparing. That way, the case is ignored. This function sorts the array in a truly alphabetical order, similar to what we've achieved with Array.CASEINSENSITIVE.

The array constant Array.RETURNINDEXEDARRAY will make sort() return an array containing the new index of each number. For example, if the original array is [3, -1, 4], sort() will return [1, 0, 2], where the 3 is the second number (index 1), -1 is the first number (index 0), and 4 is the third number (index 2) in the sorted array.

Note that the terms are slightly different in databases. In databases, myIndices is known as an index, while our array indices are known as index keys. In this book, to avoid confusion, we will avoid using these terms.

If you use the Array.UNIQUESORT constant, Flash will return 0 instead of modifying the array if two or more elements are found to be equivalent. We'll use this constant in the fourth section, when we learn about exception handling.

Sometimes, we want to sort hashes or objects inside arrays, and it would be cumbersome to use custom criteria with sort(). That's where sortOn() comes in.

sortOn treats arrays like tables in a database. In a database, a table is a collection of entities, or records, with the same record structure. Each entity has several fields, each of which stores a particular attribute of the record. Sometimes, we need to use a query to sort the fields in a particular way. Sorting keys are the fields which are used to sort. The primary sort key is first used to sort the data. When there are two or more data with the same attribute in a certain field, the secondary sort key is used to sort those data. If there are still equivalent values, the tertiary sort key is used, and so on.

Let's say we have a table containing data on several books. We want to arrange the book titles in descending order first. If there are two books with the same title, then we sort by the author's last name in ascending order, then the author's first name in ascending order, and finally the publication year in descending order. In SQL, the syntax for doing this is:

Journey to the West by Cheng'en Wu published in 1542
The Adventures of Teddy Bear by Fred Bloggs published in 2012
The Adventures of Teddy Bear by Joe Bloggs published in 1996

That looks complicated! Let's break it down, one by one.

Firstly, we created a new Array with three object literals. Each object has a title, a last name, a first name and a year. Note that each object here is an associative array.

Next, we used sortOn. The first parameter of sortOn is an array containing the property names to be used as sort keys. The 0th element, title, is the primary sorting key, the 1th element, last, is the secondary sorting key, and so on.

The second parameter is an array containing the array constants to be used for each sort. In this case, we want Array.CASEINSENSITIVE for the string fields, Array.NUMERIC for the publication year, and Array.DESCENDING the author and publication year. The bitwise OR operator is also used.

Finally, we traced the new array from the 0th element to the 2nd.

It's not so difficult once we break it down, is it? We can also use it to sort instances of MovieClips and other classes. This seemingly unuseful technique comes in handy when you have a lot of elements to be presented. For example, if you're making a game with a level editor, you want a way to display the levels. The player can sort the levels by the date of publication, rating, difficulty, etc.

We've gone through two chapters in the second section, and so far, we've only been manipulating data without any real output on the screen. That might not be too satisfying for you, so let's move on to the very heart and soul of the Flash application: the MovieClip.