WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: bottomleypotts on April 25, 2023, 08:26:56 AM

Title: Map Errors
Post by: bottomleypotts on April 25, 2023, 08:26:56 AM
I seem to be having a problem with maps. Maybe it's just the way I am using them.

Code (winbatch) Select
mymap=MapCreate(`1,|2,|3,|a,`,`,`,`|`)
If MapKeyExist(mymap,1) Then   Message(``,`Seems like it worked`)
If MapKeyExist(mymap,`1`) Then   Message(``,`Seems like it worked too`)
e=mymap[1]
f=mymap[`1`]


This seems to work, as the map is generated and I can see all the variables in the debugger, but both e and f generate errors.

Code (winbatch) Select
mymap=MapCreate()
mymap[1]=``
e=mymap[1]
f=mymap[`1`]


Where this works for both e and f.

Is it documented that I not be using MapCreate to initialize maps with empty variables?
Title: Re: Map Errors
Post by: td on April 25, 2023, 08:52:44 AM
In the first example you are creating a map with an undefined value in a key/value pair and the error is self-explanatory. An empty string is not the same data type as an undefined value...

The keys of key/value pairs are always strings so the interpreter will attempt to convert any key placed within an existing map's brackets to a string. It can not make that assumption about values because a value can be any datatype except another array or map.
Title: Re: Map Errors
Post by: bottomleypotts on April 25, 2023, 04:42:03 PM
I'm not quite sure I understand why the first example creates a map with undefined values, I thought that I'm creating a map with a string. Thanks for the explanation.
Title: Re: Map Errors
Post by: td on April 26, 2023, 06:51:21 AM
If you want a map with empty strings for values, as in "", specify that in the initialization list. Again, an empty string is not equivalent to an undefined value.