views:

221

answers:

1

Hello,

I am using a Dictionary in ActionScript as a queue, sort of, still reading most of the time as an associative container, but I need one time to make a loop to run through the whole dictionary, such as for (var key:String in queue) . Inside this for loop I perform some actions on an element and then call delete on that key.

My issue is that I would like to wait for an Event before fetching the next element in this queue. Basically my for loop runs too fast. I would like to fetch the next key all the time, but I know there is no built in method.

A solution I thought is to add a break to the loop, as the for.. in will automatically fetch the next key, but it would be a loop which always executes one time, simply to fetch the next key. This sounds a bit counter intuitive.

I hope my problem makes sense and I really look for some better ideas than what I currently have. Thanks for your help!

Rudy

A: 

haha, that is an interesting problem indeed. there doesn't seem to be any way of retrieving a list of keys from the dictionary, short of the method you've outlined. an alternative would be caching the list of the keys on the first run, but that might get out of sync and is quite untidy anyway.

i guess my main question is why can't you do what you want to do with a list? if you need 2 values (eg strings) then just make a list of objects :)

oedo
Thanks for your answer.What happens is I store an ID that references to an object. Most of the times, I pass in an ID, and lookup the object (so I really use it as an associative container). But at times I need to iterate, so it becomes really a queue/array, and it has to be in the order the elements were order. To add another complexity, the keys are of course unique, they are incrementing integers starting at 0, but some of those entries might have been removed. We could have an element with the key "0", "10", "11", etc... This makes could potentially make it slow for me to iterate
Rudy
just by using integers. So to make it simple, the key is a unique integer value that is used to lookup the object but is also part of the object. I hope that explains why I am not using a list.
Rudy
i see. you could always maintain a list of the ids too, if you really need to. otherwise i think you're correct, you'll need to do a for...in which is only used once :)
oedo
ok. Thanks a lot for your help, I appreciate it!
Rudy
no worries, glad to be of assistance :)
oedo
I wonder how internally the Dictionary is able to know what is the first element. There must be some sort of reference to it, so that the for... in can work. If I had this reference, my issue would be solved.
Rudy
yeah, probably some sort of internal hashmap, as objects in actionscript are dynamic by nature. you're right, if you could just access that array it'd solve your problems :)
oedo
Dictionary internally keeps the keys in a list, but it's not an ordered list. That's the problem with having a `nextItem()` method, when you called it later, the key list might be in a different order than it was when you called it last. In this case, I think oedo's solution is definitely the best way to approach it.
fenomas