viewpoint-particle

Author Topic: Map Errors  (Read 390 times)

bottomleypotts

  • Jr. Member
  • **
  • Posts: 92
Map Errors
« 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
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
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

  • Tech Support
  • *****
  • Posts: 4440
    • WinBatch
Re: Map Errors
« Reply #1 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.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

bottomleypotts

  • Jr. Member
  • **
  • Posts: 92
Re: Map Errors
« Reply #2 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.

td

  • Tech Support
  • *****
  • Posts: 4440
    • WinBatch
Re: Map Errors
« Reply #3 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.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade