Map Errors

Started by bottomleypotts, April 25, 2023, 08:26:56 AM

Previous topic - Next topic

bottomleypotts

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?

td

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.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

bottomleypotts

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.

td

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.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade