V2: Error "Variable not found"

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
AHK_user
Posts: 515
Joined: 04 Dec 2015, 14:52
Location: Belgium

V2: Error "Variable not found"

21 Aug 2021, 04:01

Does somebody knows a workarround to bypass the v2 "Variable not found" error?

I want to convert V1 Code to V2 code, to get the same variables as a result.

The following V1 code creates variables id1, id2, id3,...
#Requires AutoHotkey v1.1.33+
WinGet, id, List,,, Program Manager


But when trying to translate this into AHK V2 Code, so it also gives back id1, id2,... (I know V2 already gives id[1], id[2],...)
It generates the error, probably because v2 does not support pseudo-arrays?
AHK V2 code :
#Requires AutoHotKey v2.0-beta.1
oid := WinGetList(,,"Program Manager",)
id := ""
For v in oid
{
id .= A_index=1 ? v : "``r``n" v
id%A_Index% := v
}
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: V2: Error "Variable not found"

21 Aug 2021, 05:01

u cant dynamically declare variables anymore. u may only dynamically reference them if they had been declared beforehand

the function call returning an array is the v2 conversion already(the last comma isnt needed)
dmatch
Posts: 49
Joined: 02 Oct 2013, 09:56

Re: V2: Error "Variable not found"

21 Aug 2021, 09:41

swagfag wrote:
21 Aug 2021, 05:01
u cant dynamically declare variables anymore. u may only dynamically reference them if they had been declared beforehand

... snip ...
Does that mean something like this is not allowed in v2?

Code: Select all

n=integer input unknown  to the programmer at time of script creation.
loop, %n%
{
thing%A_Index%=something
}
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: V2: Error "Variable not found"

21 Aug 2021, 10:05

it means if u intend for thing%A_Index% to declare variables thing1, thing2 through thingn, no its not allowed. ud get an error
dmatch
Posts: 49
Joined: 02 Oct 2013, 09:56

Re: V2: Error "Variable not found"

21 Aug 2021, 10:21

OUCH! The use of undeclared dynamic variables is one thing that makes AutoHotkey v1.1 unique and very useful to me. I guess stuff like this will have to be accomplished thru use of dictionaries in v2 similar to the way it can be done in python.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: V2: Error "Variable not found"

21 Aug 2021, 12:27

they werent any more useful than the available alternatives, but they did cause more problem, so they were removed
guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

Re: V2: Error "Variable not found"

21 Aug 2021, 12:33

AHK_user wrote:
21 Aug 2021, 04:01
Does somebody knows a workarround to bypass the v2 "Variable not found" error?

I want to convert V1 Code to V2 code, to get the same variables as a result.

The following V1 code creates variables id1, id2, id3,...
#Requires AutoHotkey v1.1.33+
WinGet, id, List,,, Program Manager


But when trying to translate this into AHK V2 Code, so it also gives back id1, id2,... (I know V2 already gives id[1], id[2],...)
It generates the error, probably because v2 does not support pseudo-arrays?
AHK V2 code :
#Requires AutoHotKey v2.0-beta.1
oid := WinGetList(,,"Program Manager",)
id := ""
For v in oid
{
id .= A_index=1 ? v : "``r``n" v
id%A_Index% := v
}
this is gonna be a problem

guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

Re: V2: Error "Variable not found"

21 Aug 2021, 14:50

swagfag wrote:
21 Aug 2021, 14:07
what is?
writing an automatic v1 -> v2 converter for this case like OP is likely trying to do

AHK_user
Posts: 515
Joined: 04 Dec 2015, 14:52
Location: Belgium

Re: V2: Error "Variable not found"

21 Aug 2021, 15:22

Yea, I am trying to complete a v2 convertor.

For now I see two workarounds when converting:
1. Checking the rest of the code and changing every ID%Var% to ID[Var] (probably the most proper one, but complicated in some cases, and could create other errors)
2. Adding the line: static id1,id2,id3,id4,id5,id6 till 100 :D a dirty hack, but for now I am happy it the code does not breaks.

If somebody knows a better workarround, please let me know.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: V2: Error "Variable not found"

23 Aug 2021, 14:48

3. flag the line and tell the user to fix their code(optionally providing an example showing how to do that, eg pass directly to for-loop; store result in array)
AHK_user
Posts: 515
Joined: 04 Dec 2015, 14:52
Location: Belgium

Re: V2: Error "Variable not found"

24 Aug 2021, 17:29

This seems to do the trick, or can somebody think of a script where it would fail?

Code: Select all

#Requires AutoHotkey v2.0-a
text:= "
(
    test := array%sdsfs% array%sdsfs%
    array%sdsfs%sds ; do not convert
    darray%A_index% ; do not convert
    msgbox, array1 array21456479 array%sdsfs%
)"
MsgBox(ConvertPseudoArray(text,"array"))

ConvertPseudoArray(ScriptString,ArrayName){
    Loop {
        ScriptString := RegExReplace(ScriptString, "is)(^(|.*[^\w])" ArrayName ")(%(\w+)%|(\d+))(([^\w].*|)$)", "$1[$4$5]$6",&OutputVarCount) 
          
    } Until OutputVarCount = 0
    Return ScriptString
}
AHK_user
Posts: 515
Joined: 04 Dec 2015, 14:52
Location: Belgium

Re: V2: Error "Variable not found"

27 Apr 2022, 05:35

:think:
I am still thinking about this problem, as my convertor solution is quite complex and opens room for other errors.

We have a possibility to set the warning off, but I do not like that this would set it off for the whole script. :think:
#Warn VarUnset, Off

Resetting the warning later makes the script behave as before (#Warn VarUnset, msgbox)

It would be usefull to have a function to initialise a unset variable
like InitVar(Varname,Value)

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: niCode, TAC109 and 49 guests