views:

38

answers:

1

I have an array of objects which uses a delimited string as the keys. When examined in the FB4 debugger, the keys look like this:

   [+] 22477◦25220◦20.1
   [+] 22477◦25220◦20.6
   [+] 22477◦25220◦20.8
   [+] 22477◦25244◦55.1K(j)

The first two items are numeric (cast to string) but the third item in the multi-part delimited key is naturally a string -- it's like an alphanumeric library shelf reference. As expected, when you click on the [+] icon in the debugger, you can view the object associated with that string key. So far so good.

The debugger shows the keys in the (pre-sorted) order in which they were added to the array. However, when iterating the object array so:

    for (var key: String in MyAssociativeArray){
          // keys are visited not in the order displayed by the debugger
    }

the keys are returned in some other order --internal hash? My question is, how does the debugger know the order the keys were added in, and can I access that knowledge at runtime when iterating the array? I want to iterate the objects in the order in which they were added. Or do I need to maintain my own index of these keys showing the order they were added to the associative array?

   [0] 22477◦25220◦20.1
   [1] 22477◦25220◦20.6
   [2] 22477◦25220◦20.8
   [3] 22477◦25244◦55.1K(j)

Thanks

+2  A: 

The debugger sorts lists of items in an associative array lexicographically for you (ordered numerically and alphabetically) to make it easier to find what you're looking for. The list in your example is sorted this way. It's a coincidence that you added items to the associative array in the same order.

There is no way for you to discover the order in which you add items to an associative array without creating additional metadata. If you need such behavior try creating a custom class for this.

Here's an example:

import flash.utils.Dictionary;
import flash.utils.Proxy;
import flash.utils.flash_proxy;


dynamic class OrderedAssoArray extends Proxy{
    private var _items:Dictionary = new Dictionary();
    private var _idx:Array = new Array();
    use namespace flash_proxy
    override flash_proxy function setProperty(name:*, value:*):void {       
        _items[name] = value;
        _idx.push(name);
    }
    override flash_proxy function getProperty(name:*) : *{      
        return _items[name];
    }
    override flash_proxy function nextNameIndex (index:int):int {       
        return index < _idx.length ? index + 1 : 0;     
    }
    override flash_proxy function nextName(index:int):String {      
        return _idx[index-1];
    }
    override flash_proxy function nextValue(index:int):* {      
        return _items[_idx[index -1]];
    }
}

With the above you can do something like this:

        var assoArray:Object = new OrderedAssoArray();
        assoArray["key_one"] = "value_one"          
        assoArray["key_two"] = "value_two"
        assoArray["key_three"] = "value_three"
        assoArray["key_four"] = "value_four"
        for each(var item:String in assoArray){
            trace(item)
        }
charleskreps
Thank you, Charles, for the informative reply.
Tim
I'm new to stackoverflow. How does the answer become "accepted"?
charleskreps
I am new myself. I am trying to revisit the questions I asked "anonymously", i.e. without registering and using one's email address, and seeing if I can accept answers. I have clicked on the check image and hope that's all there is to it. As an unregistered person, I am not able to bump answers up in the order; it appears only registered users can do that.
Tim