Map Assign / Size

Just starting out? Need help? Post your questions and find answers here.
wayne-c
Enthusiast
Enthusiast
Posts: 335
Joined: Tue Jun 08, 2004 10:29 am
Location: Zurich, Switzerland

Map Assign / Size

Post by wayne-c »

Code: Select all

NewMap Test()
Debug MapSize(Test())

; Add a member "A" to the map
; the expected map size is 1, OK
Test("A") = 1
Debug MapSize(Test())

; Check the value of a non-existent member
; the expected map size is still 1, BUT...
; a new member "B" is automatically created and added to the map!
If Test("B")
	Debug "B <> 0"
EndIf
Debug MapSize(Test())
Is this correct behavior or a bug? In my opinion the map size should only increment when assigning new values...
Am I wrong or is this a bug?
As you walk on by, Will you call my name? Or will you walk away?
User avatar
Sicro
Enthusiast
Enthusiast
Posts: 538
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

Re: Map Assign / Size

Post by Sicro »

Not a bug.

Use "FindMapElement()" to check if a map element already exists.
Image
Why OpenSource should have a license :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (syntax color scheme) :: RegEx-Engine (compiles RegExes to NFA/DFA)
Manjaro Xfce x64 (Main system) :: Windows 10 Home (VirtualBox) :: Newest PureBasic version
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Map Assign / Size

Post by Little John »

Bug or not bug: This behaviour of PB's maps is inconsistent.

See the previous discussion about this issue: viewtopic.php?f=4&t=68880
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Map Assign / Size

Post by infratec »

Code: Select all

Test("B")
adds a new element.
See the help:

As already mentioned for tests you need FindMapElement()

Bernd
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Map Assign / Size

Post by Little John »

infratec wrote:

Code: Select all

Test("B")
adds a new element.
It depends on the actual code. Sometimes it does do so, and sometimes it dosn't.

For instance, in this code

Code: Select all

NewMap Test()
Debug Test("B")        ; -> 0
Debug MapSize(Test())  ; -> 0
Test("B") does not add a new element.
The behaviour is inconsistent.

As I already wrote, this has been discussed before: viewtopic.php?f=4&t=68880
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: Map Assign / Size

Post by Josh »

Little John wrote: For instance, in this code

Code: Select all

NewMap Test()
Debug Test("B")        ; -> 0
Debug MapSize(Test())  ; -> 0
Test("B") does not add a new element.
The behaviour is inconsistent.
Regardless whether it makes sense to automatically create an entry in the map, this behaviour is not inconsistent. With the Debug in front of the line, Test("B") is a rhs-term and so no new element is assigned to the map.
Last edited by Josh on Sat Feb 16, 2019 12:32 pm, edited 1 time in total.
sorry for my bad english
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: Map Assign / Size

Post by #NULL »

Code: Select all

NewMap Test()
Debug Test("B")        ; -> 0
Debug MapSize(Test())  ; -> 0

Procedure f(i)
  Debug i
EndProcedure
f(Test("B"))        ; -> 0
Debug MapSize(Test())  ; -> 1
For the call of f() it's also not an lhs but the element gets added.
Post Reply