MapKeyExist with Numbers as Keys

Started by chrislegarth, May 22, 2020, 10:18:17 PM

Previous topic - Next topic

chrislegarth

I don't know if this is a bug or not but what I am trying to do is place a random number in the Key of a Map array.
I then want to make sure that I don't pick the same number twice for the amount of numbers I want to pick.

What I have noticed is that, if I place a number (not a string) in the Key of the Map, the MapKeyExist function does not find the Key to continue the WHILE loop to pick a new number.  I would have expected the example code below to WHILE loop forever but instead it completes the FOR/NEXT loop with only 1 key in the Map.  So it appears that the Map function does not convert numbers to strings when creating/updating Keys.  I'm creating the empty Map first to avoid the first key assignment restriction.

Can I use numbers as Map Keys? Do I have to convert the number to a string using substitution, i.e. "%num%", when using the MapKeyExist function?  Or am I missing something?

Code (winbatch) Select

;EXAMPLE CODE
Count = 5
Map = MapCreate()

for xCount = 1 to Count
while @true
num = 10
if MapKeyExist(Map, num) == @true then continue
Map[num] = "data" : xCount
break
endwhile
next
exit

td

As expected I get an infinite loop when I run your script. Which version of WinBatch are you using?
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

chrislegarth


JTaylor

Don't know if this applies to your situation but...

    "When dynamically creating a WIL map the key used in the first assignment statement creating the map cannot be a literal or variable with contents that will convert to an integer.  If the key is convertible to an integer, a regular WIL array will be created. To avoid this use the MapCreate function to create the map when there is any chance that the first assignment could be an integer.  "


Jim

td

Quote from: chrislegarth on May 22, 2020, 11:50:33 PM
I am running 2019B.

This MapKeyExsit problem was addressed 2020A. If you upgrade, it will go away. However, there is another issue involving the WIL debugger in 2020A. The debugger issue does have a simple workaround. So if you upgrade and you use the WinBatch Studio Debugger put this line at the top of your script when your script uses WIL maps.

Code (winbatch) Select
if RtStatus() == 10 then IntControl(100, 64,0,0,0)

"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

chrislegarth

Thanks for the info.  I will be upgrading here in the near future.