Wednesday, December 12, 2012

Two indexed arrays with enumeration

You can use an enumeration to make the elements easier to read so that at least one set of indices can be referred to by name instead of by number. For example, you can use an associative array of days so that each day can be accessed by an enumeration instead of by a number:
var Days:Object = {MON:0, TUE:1, WED:2, THU:3, FRI:4, SAT:5, SUN:6};
var masterTaskList:Array = new Array();
masterTaskList[Days.MON] = ["wash dishes", "take out trash"];
masterTaskList[Days.TUE] = ["wash dishes", "pay bills"];
masterTaskList[Days.WED] = ["wash dishes", "dentist", "wash dog"];
masterTaskList[Days.THU] = ["wash dishes"];
masterTaskList[Days.FRI] = ["wash dishes", "clean house"];
masterTaskList[Days.SAT] = ["wash dishes", "wash car", "pay rent"];
masterTaskList[Days.SUN] = ["mow lawn", "fix chair"];
NOTE the example uses abbreviations for each day for brevity. You could choose to use the full name of each day instead. You can access an item on any of the task lists using bracket notation. For example, the following code retrieves the second task from Wednesday’s list, and the first task from Sunday’s list.
trace(masterTaskList[Days.WED][1]); // Output: dentist
trace(masterTaskList[Days.SUN][0]); // Output: mow lawn
Programming Actionscript 3. Powered by Blogger.