Dumping key/value pairs from TMap

BlitzMax Forums/BlitzMax Programming/Dumping key/value pairs from TMap

Here's an example I whipped up while trying to figure out how to do this myself. I apologize for the ultra sloppy code

Strict

Global testmap:tmap 
Global tempstring1:String
Global tempstring2:String
testmap = New tmap
tempstring1 = "test"
tempstring2 = "54"

testmap.insert( "test", tempstring2 )
Print String(testmap.valueforkey( "test" ))

testmap.insert( "wibbleding", "66" )
Print String(testmap.valueforkey( "wibbleding" ))



Global Enumer:TEnumer
Enumer = testmap.keys()


'ObjectEnumerator() returns an enum object. NextObject() is an overridden function that exists in 
'TKeyEnum and TValueEnum, both extended from TNodeEnum. TEnumer has a field, _enum , which is of type
'TNodeEnum. TMap.Keys() and TMap.Values() both return a TEnumer whos _enum field is filled with
'either a TKeyEnum or TValueEnum object, respectively.
'And in this Case'since Enumer was created with the keys() function, it will return the key of each object in the map
'if values are desired instead, create the Enumer with tmap.values(). Then nextobject() will return 
'all of the values in the map.

While Enumer._enum.HasNext()
	Print "inside HasNext() loop"
	Print String(enumer.ObjectEnumerator().nextobject())
EndWhile




'this demonstrates how to cycle through a map and print all key,value pairs.

Global Enumer2:TNodeEnum

'here, ObjectEnumerator() inside of the TMap returns a TNodeEnum pointing to the root node of the map
Enumer2 = testmap.ObjectEnumerator()

Local tempnode:tNode


While Enumer2.HasNext()	'true while there are nodes left to traverse
	
	'NextObject returns a node but returns it as type object so it has to be cast
	tempnode = tnode(Enumer2.NextObject())	'cast the returned object to a tnode, since it is one
	Print "Key:" + String(tempnode._key)	
	Print "Value:" + String(tempnode._value)
EndWhile