TMapEnumerator

BlitzMax Forums/BlitzMax Programming/TMapEnumerator

Czar Flavius(Posted 2010) [#1]
I'm not posting this as a bug, but something to look out for. Storing the enumerator from a TMap in a temp variable and iterating through it destroys its contents, so you can only do it once. It tripped me up! (Of course getting it from map.values() again is ok)

Strict
Local map:TMap = New TMap

map.insert("key1", "value1")
map.Insert("key2", "value2")
map.insert("key3", "value3")


Local mvalues:TMapEnumerator = map.values()

Print "map"
For Local v:String = EachIn mvalues
	Print v
Next

Print "map"
For Local v:String = EachIn mvalues
	Print v
Next


map

value1

value2

value3

map



Jesse(Posted 2010) [#2]
it's not receting the enumarator that is all.
Strict
Local map:TMap = New TMap

map.insert("key1", "value1")
map.Insert("key2", "value2")
map.insert("key3", "value3")


Local mvalues:TMapEnumerator = map.values()

Print "map"
For Local v:String = EachIn mvalues
	Print v
Next
mvalues=map.values()
Print "map"
For Local v:String = EachIn mvalues
	Print v
Next



Warpy(Posted 2010) [#3]
why would you want to keep the enumerator object?


N(Posted 2010) [#4]
I'm curious about this now. What're you doing that requires you to keep a reference to the enumerator?


Kurator(Posted 2010) [#5]
Thats by default what an Enumerator should do, iterating from begin to the end of a List, Map etc. - if it reaches the end, it is still at the end of the list.

But I'm just as curious as Nilium :)


Czar Flavius(Posted 2010) [#6]
I just wanted to iterate through the same map twice in a row is all :)


Kurator(Posted 2010) [#7]
Then you have to do it like jesse wrote or:

Strict
Local map:TMap = New TMap

map.insert("key1", "value1")
map.Insert("key2", "value2")
map.insert("key3", "value3")

For Local v:String = EachIn map.values()
	Print v
Next
mvalues=map.values()
Print "map"
For Local v:String = EachIn map.values()
	Print v
Next



Grover(Posted 2011) [#8]
Erm, this is quite odd behaviour. In other languages an Enum is a const, and it doesnt 'disappear' after use, and you dont need to re-evaluate it. This does sound like a garbage collect bug or there must be a better way to define an Enum type object.

Last edited 2011


Perturbatio(Posted 2011) [#9]
In other languages an Enum is a const, and it doesnt 'disappear' after use, and you dont need to re-evaluate it.


It's not an Enum (like in C) it's an map enumerator, a class used to enumerate (or step) through items in a TMap.

Last edited 2011