Enumerate keys and values of TMap together.
BlitzMax Forums/BlitzMax Programming/Enumerate keys and values of TMap together.
| ||
I want to do something to every value in a map, but what I do with it depends on its keys too. Is there a way I can enumerate through every node in the map, getting both the key and the value each time? Or must I enumerate through the keys, and ValueForKey each key, taking more time? |
| ||
Just use valueforkey. Its not like its slow. |
| ||
It seems kind of silly having to look up the same node twice, when enumerating gives both values but you can only pick one. |
| ||
If you need both, why don't you store the key in the object itself? |
| ||
The object is an array. I just used ValueForKey. I can't be bothered messing around with it anymore. It would have been nice, if enumerating keys did them in ascending order (perhaps it does, I didn't get to that part) and so I could only process the first 50 lowest nodes without messing around with anything else. Last edited 2011 |
| ||
For Local n:TNode = Eachin someMap Local key:Object = n.Key() Local value:Object = n.Value() Next |
| ||
Really??? |
| ||
Works on this :-SuperStrict Local M:TMap = CreateMap() MapInsert( M , "Key1" , "Value1" ) MapInsert( M , "Key2" , "Value2" ) For Local n:TNode = EachIn M Local key:Object = n.Key() Local value:Object = n.Value() Print "Key :- " + String(key) Print "Value :- " + String(value) Next |
| ||
Cool thanks Tommo ;D Last edited 2011 |
| ||
How's that compare speed-wise to using the key enumerator/valueForKey? |
| ||
From what I can understand in the code, the key and value enumerators just extend the node enumerator but only return one of the two data values. So enumerating the nodes will be faster than enumerating the nodes and then doing a lookup. I actually found the node enumerator before, but couldn't figure out how to use it (EachIn Map was too obvious!) and just assumed it was used as base code. |