# Problem with Maps

## Recommended Posts

Hello

I initialize elements of a Map (map of counters) to 0, then call a function to increment one element. This element  is well incremented, but another is emptied.

I wrote the following piece of code to reproduce the problem :

```Main()

Func Main()
Local \$mTP[]
\$mTP=GetNewTP("P1")
IncrementStat(\$mTP, "fl_a2_1b")
EndFunc

Func GetNewTP(\$sName)
Local \$mTP[]
\$mTP["name"]  = \$sName
\$mTP["stats"] = GetNewStatsMap()
Return \$mTP
EndFunc

Func GetNewStatsMap()
Local \$mStats[]
Local \$a1 = ["pf", "fl", "tu", "ri"]
Local \$a2 = ["a1", "a2"]
Local \$a3 = ["fo", "ch", "ca", "1b", "2b", "3b", "xb"]
\$mStats["hn"] = 0
For \$s1 In \$a1
For \$s2 In \$a2
For \$s3 In \$a3
\$sStatCode = \$s1 & "_" & \$s2 & "_" & \$s3
\$mStats[\$sStatCode] = 0
Next
Next
Next
Return \$mStats
EndFunc

Func IncrementStat(ByRef \$mTP, \$sStatCode)
Local \$mStats[]
\$mStats = \$mTP["stats"]
ConsoleWrite( " BEFORE =====>> \$mStats[fl_a2_xb] = " & \$mStats["fl_a2_xb"] & @CRLF)
ConsoleWrite( " BEFORE =====>> \$mStats[" & \$sStatCode & "] = " & \$mStats[\$sStatCode] & @CRLF)
\$mStats[\$sStatCode] = \$mStats[\$sStatCode] + 1
ConsoleWrite( " AFTER  =====>> \$mStats[" & \$sStatCode & "] = " & \$mStats[\$sStatCode] & @CRLF)
ConsoleWrite( " AFTER  =====>> \$mStats[fl_a2_xb] = " & \$mStats["fl_a2_xb"] & @CRLF)
\$mTP["stats"] = \$mStats
EndFunc```
Quote

BEFORE =====>> \$mStats[fl_a2_xb] = 0
BEFORE =====>> \$mStats[fl_a2_1b] = 0
AFTER  =====>> \$mStats[fl_a2_1b] = 1
AFTER  =====>> \$mStats[fl_a2_xb] =

In the real program, 2 elements are emptied but I could not reproduce exactly the same behaviour here.

Best regards

##### Share on other sites

I fix your code. You have to know that before you add some item to array first set array dimesion.

`Dim \$aArray[2] ; Create an array with 2 rows`

It's ur code

```Main()

Func Main()
Dim \$mTP[0]
\$mTP=GetNewTP("P1")
IncrementStat(\$mTP, "fl_a2_1b")
EndFunc

Func GetNewTP(\$sName)
Dim \$mTP[2]
\$mTP["name"]  = \$sName
\$mTP["stats"] = GetNewStatsMap()
Return \$mTP
EndFunc

Func GetNewStatsMap()
Dim \$mStats[57]
Local \$a1 = ["pf", "fl", "tu", "ri"]
Local \$a2 = ["a1", "a2"]
Local \$a3 = ["fo", "ch", "ca", "1b", "2b", "3b", "xb"]
\$mStats["hn"] = 0
For \$s1 In \$a1
For \$s2 In \$a2
For \$s3 In \$a3
\$sStatCode = \$s1 & "_" & \$s2 & "_" & \$s3
\$mStats[\$sStatCode] = 0
Next
Next
Next
Return \$mStats
EndFunc

Func IncrementStat(ByRef \$mTP, \$sStatCode)
Dim \$mStats[2]
\$mStats = \$mTP["stats"]
ConsoleWrite( " BEFORE =====>> \$mStats[fl_a2_xb] = " & \$mStats["fl_a2_xb"] & @CRLF)
ConsoleWrite( " BEFORE =====>> \$mStats[" & \$sStatCode & "] = " & \$mStats[\$sStatCode] & @CRLF)
\$mStats[\$sStatCode] = \$mStats[\$sStatCode] + 1
ConsoleWrite( " AFTER  =====>> \$mStats[" & \$sStatCode & "] = " & \$mStats[\$sStatCode] & @CRLF)
ConsoleWrite( " AFTER  =====>> \$mStats[fl_a2_xb] = " & \$mStats["fl_a2_xb"] & @CRLF)
\$mTP["stats"] = \$mStats
EndFunc```

Output from scite:

```; BEFORE =====>> \$mStats[fl_a2_xb] = 0
; BEFORE =====>> \$mStats[fl_a2_1b] = 0
; AFTER  =====>> \$mStats[fl_a2_1b] = 1
; AFTER  =====>> \$mStats[fl_a2_xb] = 1```

##### Share on other sites

@Ascer Haven't used maps much, but why would f1_a2_xb = 1?  Its not being set in IncrementStat function also noticed if you changed that second to last line to:

`ConsoleWrite( " AFTER  =====>> \$mStats[fl_a2_xb] = " & \$mStats["abcdefg"] & @CRLF)`

You'll still get 1

Am I missing something?

##### Share on other sites

I know that code make no sense since:

```Dim \$aArray[2]
\$aArray["param"] = "name"
\$aArray["stat"] = 1```

"param" and "stat" in this situation will read as Integer 0. Such as declare is using in Python.

I will not sure about klpc idea. This can be done with

`ObjCreate("Scripting.Dictionary")`

##### Share on other sites

@all the string answers

you are treating it as an array, and when specifying array indices it expects a number where you have placed a string, autoit makes your string the number 0.

`msgbox(0, '' , 0 = "one")`

I dont think that GetNew nor Incrementstat functions are tenable in their current form, you may have to pick another way, I would first try mapappend instead of incrementing a variable

searching my map threads will show some obscure map/dictionary ways to do KV pairs.

Edited by iamtheky
semantics

```,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)```

##### Share on other sites

Using code from first post, if you check the vartype for "fl_a2_xb" it's set as a "keyword" rather than Int32, if you change "xb" to anything else for example:

`Local \$a3 = ["fo", "ch", "ca", "1b", "2b", "3b", "4b"]`

You'll notice the issue disappears, what constitutes a "keyword" type, have no idea, as I mentioned haven't really much experience with Maps, maybe @iamtheky will have an idea.

For reference modified the IncrementStat function to:

```Func IncrementStat(ByRef \$mTP, \$sStatCode)
Local \$mStats[]
\$mStats = \$mTP["stats"]
Local \$aMapKeys = MapKeys(\$mStats)
For \$i = 0 To UBound(\$aMapKeys) - 1
ConsoleWrite(@CRLF & " BEFORE =====>> \$mStats[" & \$aMapKeys[\$i] & "] = " & \$mStats[\$aMapKeys[\$i]] & " - " & VarGetType(\$mStats[\$aMapKeys[\$i]]) & @CRLF)
If \$aMapKeys[\$i] = \$sStatCode Then \$mStats[\$sStatCode] = \$mStats[\$sStatCode] + 1
ConsoleWrite( " AFTER  =====>> \$mStats[" & \$aMapKeys[\$i] & "] = " & \$mStats[\$aMapKeys[\$i]] & " - " & VarGetType(\$mStats[\$aMapKeys[\$i]]) & @CRLF)
Next
\$mTP["stats"] = \$mStats
EndFunc```

Edited by Subz
Updated IncrementStat function for debugging

##### Share on other sites

I dont know what's going on quite yet, I would want to fix all this nomenclature to just say \$sKey , \$sValue and build standard looking map functions.  If the goal is to take an array and mapify it, scripting dictionary might add more flexibility.

Since maps are still only beta, it's also not a bad idea to keep this thread in the pocket

Edited by iamtheky

```,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)```

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account