• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 11. Arrays > Named Array Elements

11.6. Named Array Elements

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.


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint