Map access should not always result in creation of element

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Rinzwind
Enthusiast
Enthusiast
Posts: 636
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

Map access should not always result in creation of element

Post by Rinzwind »

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.
User avatar
jacdelad
Addict
Addict
Posts: 1431
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Map access should not always result in creation of element

Post by jacdelad »

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
User avatar
Demivec
Addict
Addict
Posts: 4085
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Map access should not always result in creation of element

Post by Demivec »

Rinzwind wrote: Sun Oct 24, 2021 4:06 am if mymap("test") = 10 ...
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)
User avatar
STARGÅTE
Addict
Addict
Posts: 2067
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Map access should not always result in creation of element

Post by STARGÅTE »

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:

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())
Further topics:
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 moreTypeface - Sprite-based font include/module
Rinzwind
Enthusiast
Enthusiast
Posts: 636
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

Re: Map access should not always result in creation of element

Post by Rinzwind »

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.
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2056
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Re: Map access should not always result in creation of element

Post by Andre »

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... :wink:
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
Rinzwind
Enthusiast
Enthusiast
Posts: 636
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

Re: Map access should not always result in creation of element

Post by Rinzwind »

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."
Post Reply