logo

 

     
 
Home
Site Map
Search
 
:: Bitwise Courses ::
 
Bitwise Dusty Archives
 
 
 

rss

 
 

ruby in steel

learn aikido in north devon

Learn Aikido in North Devon

 


Section :: ActionScript
- Format For Printing...

ActionScript 1 - Associative Arrays

Or how to make a Hash of an Object
Monday 13 October 2008.
 

In this new series, I’ll be taking a look at some of the fundamental features of ActionScript programming. In part one, I’ll try to find out why the root class of the ActionScript class hierarchy is a Hash...


You can download a 60-day trial of Flex Builder from: http://www.adobe.com/products/flex/
You can find all the code in this article ‘ready to run’ in the download code archive, as1.zip. If you are using Adobe Flex Builder 3, you can open this project without unzipping the archive first: do this by selecting File/Import/Flex Project. With ‘Archive file’ selected, click Browse and locate the zip archive. Select Open. Then click Finish. If you are using some other editor or IDE you may need to open the zip file before importing the project. To run a project, select an mxml file in the Flex Navigator and click an item on the Run menu.

Key Objectives

In ActionScript, the basic class from which all other classes are derived is called Object - and it is very similar to what some other languages call a Hash or a Dictionary. In other words, an Object may contain collections of two-element items in which the first element is a ‘key’ and the second element is a ‘value’. In ActionScript terminology, this type of ‘keyed’ collection is known as an associative array. It is similar to a standard array apart from the fact that the index of an item is given by the key rather than by a numerical position.

The code below show how to create a normal (indexed) array and an associative (keyed) array and how to find the string ‘orange’ in each of these and assign it to a string variable:

var anArray:Array = ["apples","oranges","pears"];
var anAssociativeArray:Object = {crunchy:"apples", tangy:"oranges", juicy:"pears"};

var s1:String = anArray[1];
var s2:String = anAssociativeArray["tangy"];

The result of the above is that s1 is assigned the value at index 1 from the array (in ActionScript, arrays are indexed from 0) and s2 is assigned the value associated with the key ‘tangy’ from the associative array. In other words, both s1 and s2 are assigned the string, “oranges”.

Incidentally, the code which I used to initialize the associative array is called an ‘object literal’ and these can only be used to initialize instances of the Object class (not of descendent classes) just as string literals can only be used to initialize instances of the String class.

Associative array (or Hash) structures have many uses but why, you may wonder, would they be provided in the base class rather than in some specialized descendant? That is something that puzzled me when I started working with ActionScript. In fact, it makes a bit more sense when you understand how ‘properties’ are implemented.

Run the sample code in a browser

Members Only

It turns out that the names of the members of an object are keys in an associative array. So, while it may be normal to access properties such as x and y of a TextArea named ‘ta’ using this sort of ‘dot notation’...

ta.x;
ta.y;

You can also access those properties using their names as keys...

ta["x"];
ta["y"];

Before going any further, I should say that I am using the term ‘properties’ here rather loosely to mean the ‘members’ of an object (or class) - not only ‘getter’ and ‘setter’ properties but also variables and constants. ActionScript itself uses the term ‘property’ to mean two different things. The Adobe Flex 3 Programming ActionScript 3 reference guide explains this:

In discussions of the ActionScript object model, the term property means anything that can be a member of a class, including variables, constants, and methods. This differs from the way the term is used in the ActionScript 3.0 Language and Components Reference, where the term is used more narrowly and includes only class members that are variables or are defined by a getter or setter method. [1]

At any rate, for our purposes, it suffices to consider only those members that are publically accessible - that is, which can be accessed using dot notation. The point I want to make is that dot notation is just an alternative syntax to indexing into an associative array using a key. Once you understand that, the otherwise eccentric-sounding decision to make the Object class an associative array should make more sense: Objects need members (properties, variables etc.) and, in ActionScript, a class maintains a ‘lookup list’ of those members in the form of key-value pairs: the key is the member’s name, the value is its data.

To access a static (‘class-level’) constant, XXX, of the MyClass class I could use either of the following:

MyClass.XXX;
MyClass["XXX"];

You can create new ‘properties’ for an object (or any array) using dot-syntax or square-bracket syntax:

arr2["a"] = "arr2: one";
arr2["b"] = "arr2: two";
arr2.c = "arr2: three";

You might think that, since all classes descend from Object, you should be able to create new properties for objects of all types. Not so! This won’t work with most objects (try it with a Button, for example, and you will get nowhere). It turns out that most classes are ‘sealed’ meaning that their properties and methods are limited to those that were available at compile-time and no more can be added. Object and Array are both ‘dynamic’ classes, however, which means that new properties can be added. If you want to add more properties to an existing class you need to create a dynamic descendent by preceding the class definition with the keyword dynamic. this is what I have done in the MyButton class (in the file MyButton.as in the code archive):

public dynamic class MyButton extends Button
{
        public function MyButton()
        {
        }
}

This code works:

public var myBtn:MyButton = new MyButton();
myBtn["a"] = "btn: one";
myBtn["b"] = "btn: two";
myBtn.c = "btn: three";

However, if myBtn were created as a regular (non-dynamic) Button, any attempts to add new properties would fail.

Deleting Properties

One more piece needs to be fitted into this puzzle. Having created new properties, how can I delete them? Associative arrays, perhaps due to the fact that they exist at the ‘base’ of the entire class hierarchy, are not very ‘intelligent’ objects: they don’t have a whole load of methods with which they can be manipulated. There is not, for example, a ‘delete’ method that lets you remove a specific key-value pair. Instead, ActionScript provides a brute-force way of doing this using the ‘delete’ keyword.

This is how to delete an item with the key (the ‘property name’) “b”:

delete myOb["b"];

Note that you can also delete items from a specific index in an array in this same way:

delete arr[1];

However, while this removes the value, it replaces it with the special value ‘undefined’. So, for example, if the string “oranges” were at index 1 an array of 3 items, the above code would replace that string with the ‘undefined’ value. However, the length of the array would still be 3. See my sample code for an example of this, then refer to pages 147-148 of the Flex 3 Programming ActionScript 3 guide for more information.

Incidentally, ActionScript 3 also has a Dictionary class which is a special variant of an associative array that can use any type of object as a key rather than just strings. I’ll be using collections of various types in future articles in this series.

[1] Adobe Flex 3 Programming ActionScript 3 - Chapter 5: Classes / Class property attributes

AddThis Social Bookmark Button

Source Code


15 kb

Forum

  • ActionScript 1 - Associative Arrays
    21 November 2010, by fred

    Nice, You should also mention that a good test for existance of a key in such an associative array is with "===" operator: var myKey:String = "name"; if(hash[myKey] === undefined) Alert("Key " + myKey + " not found.");

  • ActionScript 1 - Associative Arrays
    24 August 2009, by sanoran triamesh

    ... Man I should have read this article sooner. I spent quite a long time trying to figure out how ’Flex 3’ can pull data out of an ’Object’ but I can’t, because I kept trying to cast it to an Array of Maps (which is what the debugger was showing me). It seems the folks at Adobe have taken artistic liberty with the English language, whereby and ’Object’ is actually an Array (associative or otherwise), but they insist on calling it an Object! I will not mention the numerous ’pleasantaries’ I cast off to the guy who made the decision to do this.

    Thanks for your article though,-it should be on the first page of all actionscript documentation.

    • ActionScript 1 - Associative Arrays
      9 September 2009

      Good article. Thanks.


Home