Page 1 of 1
[4.51] Using @ to get pointer while creating new map element
Posted: Tue Feb 08, 2011 1:31 pm
by cxAlex
German thread:
http://www.purebasic.fr/german/viewtopic.php?t=23835
If you are using @ to get the pointer to a map-element while creating it, the map element is not created.
Code: Select all
NewMap myMap.i()
Debug @myMap("haha")
ForEach myMap()
Debug MapKey(myMap())
Next
Working:
Code: Select all
NewMap myMap.i()
myMap("haha")
Debug @myMap()
myMap("lol")
Debug @myMap()
ForEach myMap()
Debug MapKey(myMap())
Next
Greets, Alex
Re: [4.51] Using @ to get pointer while creating new map ele
Posted: Wed Feb 09, 2011 12:43 am
by Demivec
cxAlex wrote:If you are using @ to get the pointer to a map-element while creating it, the map element is not created.
Here is documentation from AddMapElement() in the Map library:
This function isn't mandatory when dealing with maps, as elements are automatically added when affecting a value to them.
I would deduce that you didn't create a map-element because you didn't affect a value to it. You can't get the address of a new map element with the @ operator at the time of assignment because the @ operator cannot be used on the left side of the equation (i.e. can't use '@myMap("lol") = 3') and this isn't syntactically correct for any portion of the left side of an equation either.
The proper way would be to use the AddMapElement() command because it performs the assignment/creation and returns the address.
Code: Select all
NewMap myMap.i()
Debug @myMap("haha") ;element hasn't been assigned anything, thus not created yet
Debug AddMapElement(myMap(),"lol") ;this is the correct way to create a map element and return its address
ForEach myMap()
Debug MapKey(myMap())
Next
By the way, the address that is returned for an uncreated element is always the same, no matter what the key is. I am not sure what the address points to. I think that is a bug that probably needs to be addressed. I would suggest it = 0 if the element doesn't exist.
Re: [4.51] Using @ to get pointer while creating new map ele
Posted: Wed Feb 09, 2011 8:16 am
by IceSoft
It works if you set a 'default' value calling myMap("haha") before you start the foreach
Code: Select all
NewMap myMap.i()
Debug @myMap("haha")
myMap("haha")
myMap("lol")
Debug @myMap()
ForEach myMap()
Debug MapKey(myMap())
Debug myMap()
Next