Enumerate keys and values of TMap together.

BlitzMax Forums/BlitzMax Programming/Enumerate keys and values of TMap together.

Czar Flavius(Posted 2011) [#1]
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?


GfK(Posted 2011) [#2]
Just use valueforkey. Its not like its slow.


Czar Flavius(Posted 2011) [#3]
It seems kind of silly having to look up the same node twice, when enumerating gives both values but you can only pick one.


Gabriel(Posted 2011) [#4]
If you need both, why don't you store the key in the object itself?


Czar Flavius(Posted 2011) [#5]
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


Tommo(Posted 2011) [#6]
For Local n:TNode = Eachin someMap
	Local key:Object = n.Key()
	Local value:Object = n.Value()
Next



Czar Flavius(Posted 2011) [#7]
Really???


col(Posted 2011) [#8]
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



Czar Flavius(Posted 2011) [#9]
Cool thanks Tommo ;D

Last edited 2011


GfK(Posted 2011) [#10]
How's that compare speed-wise to using the key enumerator/valueForKey?


Czar Flavius(Posted 2011) [#11]
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.