Dumping key/value pairs from TMap

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

Anyone? I've tried anything.
The iterators work fine for dumping just the keys, but otherwise I'm lost :)

Thanks in advance

If you've got the keys, can't you just look up the values?

What do you mean by 'dumping'?

I mean retrieving the keys in a list.
But yes I could look up the values, but in other languages you can get a value/pair struct for each. Oh well :)

Maybe this helps you:

Type TTest
	Field ID:Int
	Field Name:String
	
	Function Create:TTest(ID:Int , Name:String)
		Local T:TTest = New TTest
		T.ID = ID
		T.Name = Name
		Return T
	End Function
	
	Method ToString:String()
		Return ID + " , " + Name
	End Method
	
	
End Type

Local Map:TMap = New TMap

Map.Insert("test1" , TTest.Create(1 , "Test1") )
Map.Insert("test2" , TTest.Create(2 , "Test2") )
Map.Insert("test3" , TTest.Create(3 , "Test3") )
Map.Insert("test4" , TTest.Create(4 , "Test4") )
Map.Insert("test5" , TTest.Create(5 , "Test5") )

'Getting all Keys

For Local S:String = EachIn Map.Keys()
	Print S
Next


'Getting All Values

For Local T:TTest = EachIn MAp.Values() 
	Print T.ToString()
Next

'Receiving The Node for 1 special Key

Local N:TNode = Map._FindNode("test3")

Print String(N._Key) + " : " + TTest(N._Value).ToString()


Hi,

Yes, the 'new' TMap doesn't have a way of traversing Key/Value pairs yet, as I realized today at work!

Will be fixed soon, but in the meantime you're stuck with using ValueForKey() inside a key loop.

Any idea when we might expect this? Or a suggestion of how I could fix it myself by extending TMap? I've hit a bit of a dead end in my work because of this, and I can't see any way around it.

EDIT: Actually, forget extending it. For now, if I could just flat copy it lock, stock to a new module and make some changes, it would get me through until it's fixed.

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


Ah, thanks for the example and explanation. We're safe using fields prefixed with _ are we? I thought perhaps they were subject to change or something.

if fields with _ would be a problem, many 3rd party stuff would get in serious trouble as most (including me) use it to mark something as private :)

That was precisely my point. If they're "private", we shouldn't be using them, and they may be subject to change.