a = mymap("test")
if mymap("test") = 10 ...
These statements should not automatically create an empty key "test" if it does not exist ... This should only happen when mymap is assigned a value (mymap("test") = 10). At the moment we need a combination of findmapelement and return active element to make sure no needless keys are created.
Map access should not always result in creation of element
Re: Map access should not always result in creation of element
That's something that hugs me too, but you can circumvent it by using FindMapElement() before accessing it.
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
Re: Map access should not always result in creation of element
Why don't you just use a macro?
Code: Select all
Macro mapTest_mcr(_map_, _key_, _comparison_, _value_)
(FindMapElement(_map_, _key_) And _map_ _comparison_ _value_)
EndMacro
If mapTest_mcr(mymap(), "sat", <=, 5)
Re: Map access should not always result in creation of element
One can discuss, if an element should always be created when accessing or just when assigning.
The problem at the moment is, that is inconsistent like shown in this example:
Further topics:
The problem at the moment is, that is inconsistent like shown in this example:
Code: Select all
Define NewMap MyMap.s()
Procedure ViewMapElements(Map MyMap.s())
If MapSize(MyMap())
ForEach MyMap()
Debug " MyMap("+MapKey(MyMap())+") = "+MyMap()
Next
Else
Debug " Empty"
EndIf
EndProcedure
Debug "Get Element:"
ClearMap(MyMap())
Define String.s = MyMap("A") ; No element added
ViewMapElements(MyMap())
Debug "Get Element in Function:"
ClearMap(MyMap())
Define Integer.i = Val(MyMap("A")) ; Element added
ViewMapElements(MyMap())
Debug "Get Element in Operation:"
ClearMap(MyMap())
Define String.s = MyMap("A") + MyMap("B") ; No element added
ViewMapElements(MyMap())
Debug "Get Element in Function:"
ClearMap(MyMap())
FindString(MyMap("A"), MyMap("B")) ; Element added
ViewMapElements(MyMap())
Debug "Get Element in If:" ; Element added
ClearMap(MyMap())
If MyMap("A")
EndIf
ViewMapElements(MyMap())
Debug "Get Element as Pointer:" ; No element added
ClearMap(MyMap())
Define *Element = @MyMap("A")
ViewMapElements(MyMap())
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and more ― Typeface - Sprite-based font include/module
Lizard - Script language for symbolic calculations and more ― Typeface - Sprite-based font include/module
Re: Map access should not always result in creation of element
Ah so it's even worse than I thought it was. Fred, action time. Or open source the libraries
I would say, only create on assignment. When comparing/retrieving value, return element when it exists or default value if not (equals empty string or 0). No point in adding empty element, which can quickly add up if you are not aware of it.
I would say, only create on assignment. When comparing/retrieving value, return element when it exists or default value if not (equals empty string or 0). No point in adding empty element, which can quickly add up if you are not aware of it.
- Andre
- PureBasic Team
- Posts: 2071
- Joined: Fri Apr 25, 2003 6:14 pm
- Location: Germany (Saxony, Deutscheinsiedel)
- Contact:
Re: Map access should not always result in creation of element
We even had a discussion about this topic here:
viewtopic.php?p=519406#p519406
Just remembered that I was the initiator of it back then...
viewtopic.php?p=519406#p519406
Just remembered that I was the initiator of it back then...
Re: Map access should not always result in creation of element
The manual says only assignment creates an element, which is definitely not the case currently. Its wording is somewhat ambiguous btw, "assignment" of what, one assumes to the map.
"When using a new key for an assignment, a new element is automatically added to the map. If another element with the same key is already in the map, it will be replaced by the new one. Once an element as been accessed or created, it becomes the current element of the map, and further access to this element can be done without specify the key. This is useful when using structured map, as no more element lookup is needed to access different structure field."
"When using a new key for an assignment, a new element is automatically added to the map. If another element with the same key is already in the map, it will be replaced by the new one. Once an element as been accessed or created, it becomes the current element of the map, and further access to this element can be done without specify the key. This is useful when using structured map, as no more element lookup is needed to access different structure field."