Numbered elements in an array are usually uniform in nature. For example, in an array of usernames, each element is a single user's name. But sometimes we'll want to store a bunch of related information in an array, for convenience, when the elements are not necessarily uniform. For example, in an array describing our computer, one element might be the processor speed, another might be the amount of RAM, and another might be the name of the operating system. The elements are related by virtue of describing our computer system, but the elements themselves are not uniform. Therefore, describing the elements as items 0, 1, and 2 may be accurate, but it isn't very descriptive. Although elements are usually numbered, fortunately, they can also be named. Named elements allow us to refer to elements in an array by a meaningful label—such as "processorSpeed", "ramInMB", or "osName"—instead of an integer. Not only does this make our code more legible, it also means we can access an element in an array without knowing its position. In Lingo, arrays that support named elements are called property lists. In many languages, they are known as hashes; in Perl, they are also called associative arrays.
Unlike in some languages, which allow an array element to be accessed by either name or number, a named ActionScript element cannot be accessed by number. Furthermore, named array elements cannot be manipulated by the Array methods (push( ), pop( ), etc., covered later in this chapter) and are not considered part of the array's numbered element list. For example, an array with one named element and two numbered elements will have a length of 2, not 3. To access all the named elements in an ActionScript array, therefore, we must use a for-in loop (discussed in Chapter 8), which lists both named and numbered elements.
|
To add an element that can later be retrieved by name, we use the familiar square brackets, with a string in place of the index number, on an existing array:
arrayName[elementName] = expression
where elementName is a string. For example:
var importantDates = new Array(); // Create an empty array importantDates["dadsBirthday"] = "June 1"; // Add a named element importantDates["mumsBirthday"] = "January 16"; // Add another named element
Remember to create an empty array using the new Array( ) constructor before trying to add named elements to it. We can also use the dot operator to specify a named array element, as follows:
arrayName.elementName = expression
In this case, elementName must be an identifier, not a string. For example:
var importantDates = new Array(); importantDates.dadsBirthday = "June 1"; importantDates.mumsBirthday = "January 16";
Assuming that we know an element's identifier (for example, dadsBirthday in the importantDates array), we can access it in one of two ways:
var goShopping = importantDates["dadsBirthday"]; var goShopping = importantDates.dadsBirthday;
Just as is the case when assigning a value to a named element, when accessing an element with square brackets, elementName must be a string or an expression that yields a string. Likewise, when used with the dot operator, elementName must be an identifier (i.e., the element's name without quotes), not a string. Note that when square brackets are used, elementName need not conform to the rules of forming a legal identifier. For example, the following elementName is not a legal identifier, but is permitted by the [ ] operator:
importantDates["dad's Birthday"] = "June 1";
The equivalent dot operator expression generates a compile-time error:
importantDates.dad's Birthday = "June 1"; // ERROR!
As a matter of good form, you should always avoid using illegal identifiers as named elements, despite the flexibility of the [ ] operator.
To rid an array of an unwanted named element, we use the delete operator, which we introduced in Chapter 5:
delete arrayName.elementName
Deleting a named element destroys both the element value and the element container, freeing up any memory being occupied by the element and its contents. (Contrast this with the delete operator's behavior for numbered elements, where it simply clears the value but leaves the container intact.)