Issue with multidimension array Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
AndyK70
Posts: 4
Joined: 27 Oct 2021, 06:18

Issue with multidimension array

Post by AndyK70 » 27 Oct 2021, 06:31

I'm fairly new to AutoHotkey and I'm still confused when to use %varname% and when varname only, so I think that could be part of my problem why that following code does not store values in the array.
What am I doing wrong?

Code: Select all

MonListe[1]["MonName"] := "BlaBla" ; Initialize as global array
;check Monitor Setup
sMsg := ""
SysGet, MonCount, 80
SysGet, MonPrimary, MonitorPrimary
Loop, %MonCount% {
    SysGet, MonName, MonitorName, %A_Index%
    MonListe[%A_Index%]["MonName"] := MonName
    SysGet, MonWA, MonitorWorkArea, %A_Index%
    MonListe[%A_Index%]["WATop"] := MonWATop
    MonListe[%A_Index%]["WALeft"] := MonWALeft
    MonListe[%A_Index%]["WABottom"] := MonWABottom
    MonListe[%A_Index%]["WARight"] := MonWARight
    sMsg .= "`nMonitor:`t#" . A_Index . "`nName:`t" . MonListe[%A_Index%]["MonName"] . "`nLeft:`t" . MonListe[%A_Index%]["WALeft"] . "`nTop:`t" . MonListe[%A_Index%]["WATop"] . "`nRight:`t" . MonListe[%A_Index%]["WARight"] . "`nBottom:`t" . MonListe[%A_Index%]["WABottom"] . "`n"
}
ListVars
MsgBox, %sMsg%
Exit   
grafik.png
grafik.png (2.83 KiB) Viewed 284 times
User avatar
boiler
Posts: 16770
Joined: 21 Dec 2014, 02:44

Re: Issue with multidimension array  Topic is solved

Post by boiler » 27 Oct 2021, 07:10

You didn’t initialize the array or the sub-arrays properly, and you used % signs around variables as elements of the array when you shouldn’t have. Fixed:

Code: Select all

MonListe := []
;check Monitor Setup
sMsg := ""
SysGet, MonCount, 80
SysGet, MonPrimary, MonitorPrimary
Loop, %MonCount% {
    SysGet, MonName, MonitorName, %A_Index%
	MonListe[A_Index] := []
    MonListe[A_Index]["MonName"] := MonName
    SysGet, MonWA, MonitorWorkArea, %A_Index%
    MonListe[A_Index]["WATop"] := MonWATop
    MonListe[A_Index]["WALeft"] := MonWALeft
    MonListe[A_Index]["WABottom"] := MonWABottom
    MonListe[A_Index]["WARight"] := MonWARight
    sMsg .= "`nMonitor:`t#" . A_Index . "`nName:`t" . MonListe[A_Index]["MonName"] . "`nLeft:`t" . MonListe[A_Index]["WALeft"] . "`nTop:`t" . MonListe[A_Index]["WATop"] . "`nRight:`t" . MonListe[A_Index]["WARight"] . "`nBottom:`t" . MonListe[A_Index]["WABottom"] . "`n"
}
ListVars
MsgBox, %sMsg%
Exit

See Variables and Expressions.
AndyK70
Posts: 4
Joined: 27 Oct 2021, 06:18

Re: Issue with multidimension array

Post by AndyK70 » 27 Oct 2021, 07:37

thank you. :thumbup:

What I notice reading your corrected code my main problem was the array initialization. Alright, so every dimension has to be initialized by itself, or is there a way like in other languages to initialize a multi dimension array with it's definition?
e.g.

Code: Select all

myArray[][] := Array()

The problem with using % and when not is something I need to wrap my head around. I don't get any concise rule when to use and when not.
I know in the documentation it says whenever using expressions do not use % with vars, still I'm confused.
When using it in a line like

Code: Select all

SysGet, MonWA, MonitorWorkArea, %A_Index%
Why is is necessary to put those % around A_Index or precede A_Index with a %+space?
I don't get it, sorry.
And I'm afraid of running into errors and often time debugging just because of my lack of understanding when to use the % or not.
Is there a way to make it always use % or not to use % at all? The "force expression" by preceding the variable with a % and a space is not really a solution, because it doesn't really get rid of the % to use it only shifts the usage to preceding the variable with it instead of encapsulating the variable in it.
User avatar
boiler
Posts: 16770
Joined: 21 Dec 2014, 02:44

Re: Issue with multidimension array

Post by boiler » 27 Oct 2021, 09:02

AndyK70 wrote: What I notice reading your corrected code my main problem was the array initialization. Alright, so every dimension has to be initialized by itself, or is there a way like in other languages to initialize a multi dimension array with it's definition?
e.g.

Code: Select all

myArray[][] := Array()
I don't believe there is a way to do that.

AndyK70 wrote: The problem with using % and when not is something I need to wrap my head around. I don't get any concise rule when to use and when not.
I know in the documentation it says whenever using expressions do not use % with vars, still I'm confused.
When using it in a line like

Code: Select all

SysGet, MonWA, MonitorWorkArea, %A_Index%
Why is is necessary to put those % around A_Index or precede A_Index with a %+space?
I don't get it, sorry.
It is because it is using "command" or "legacy" syntax in those cases. In legacy syntax, you wrap variables in % signs. The alternative of a % followed by a space is forcing expression syntax, so then you don't wrap it in % signs because they are not used in expression syntax (except in special cases).

AndyK70 wrote: And I'm afraid of running into errors and often time debugging just because of my lack of understanding when to use the % or not.
Is there a way to make it always use % or not to use % at all? The "force expression" by preceding the variable with a % and a space is not really a solution, because it doesn't really get rid of the % to use it only shifts the usage to preceding the variable with it instead of encapsulating the variable in it.
It's not really just shifting it to preceding it with a space. What it does is says, "what follows will be in expression syntax instead of legacy syntax even though in this case, legacy syntax is expected." In the case where your expression is a single variable, then yes, that's what it would look like. But you could use it to force an expression and have more than just a variable in your expression, such as % Min(a, b) + 3. You would only use forced expressions in command parameters that are expecting legacy syntax, which are pretty much all of them that don't state "may be an expression" in the documentation.

AHK's dual syntax (legacy vs. expression) has to be the most confusing thing for new users of the language and the worst part about it. I hated it when I started using AHK, but you eventually get used to it (but still hate it, lol). You can avoid the issue altogether by just jumping to AHK v2, which uses expression syntax only. It has recently been released as beta (as opposed to when it was alpha and script-breaking updates were being released), so it would be stable enough now to be the only version you use going forward if you want.

Back to v1, even if you want to always use expression syntax, you still have to know when to force an expression and when not to. Understand the difference between commands and functions. Again, the use of variables in command parameters typically are legacy syntax unless you force an expression, unless the documentation says that a certain parameter "may be an expression," in which case you can use expression syntax directly (so if your expression is just a variable, then it would be that variable name and nothing else at all). In function parameters, array elements and other objects, you would always use expressions. Read all the info at the link I posted as well as this from the tutorial.
AndyK70
Posts: 4
Joined: 27 Oct 2021, 06:18

Re: Issue with multidimension array

Post by AndyK70 » 27 Oct 2021, 11:41

boiler wrote:
27 Oct 2021, 09:02
AHK's dual syntax (legacy vs. expression) has to be the most confusing thing for new users of the language and the worst part about it.
Yep, it is.
Im not a professional coder but also it's not the first (script-) language in my 35years of coding I use and this sets me way off. :problem:

Thank you very much for your input and I'll have a look into ahk V2. If I don't need to get used to this dual syntax I'll try to avoid it. :mrgreen:
Post Reply

Return to “Ask for Help (v1)”