2

How can I perform the following operations on a dictionary, provided all my values are unique and unsorted.

key : value
152 : 780
87 : 688
2165 : 15
  • I want to get all keys.
  • I want to find key for value 688
  • I want to get all values.

Preferably, without a loop and without having to maintain the key-value relation in an external object.

I sometimes miss, python for these sort of things.

Fahim Akhter
  • 1,617
  • 5
  • 31
  • 49
  • 1
    I think this is what you want, answered previously in this thread: http://stackoverflow.com/questions/2386793/efficient-looping-through-as3-dictionary – Plastic Sturgeon Nov 03 '11 at 21:31

2 Answers2

2

If the keys are integer values, Array might help. It is a sparse array; so for and for each will only process keys that have been assigned a value (I have no idea about memory usage though).

So:

var list: Array = [];
list[1] = 10;
list[4] = 40;

for each(var value: int in list) trace(value);
// outputs (order can be different)
// 10
// 40

// index has to be * or String; the compiler gives an error if it is not
for(var index: * in list) trace(index);
// outputs (order can be different)
// 1
// 4

There is a indexOf method to get the key for a value; there is no simple function to get all keys though.

Josha
  • 579
  • 3
  • 7
0

You cannot do any point you want without the cost of an iteration over your Dictionary. So maintaining another object seems to be what you need to do.

mister why
  • 1,967
  • 11
  • 33