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
}
V2: Error "Variable not found"
Re: V2: Error "Variable not found"
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)
the function call returning an array is the v2 conversion already(the last comma isnt needed)
Re: V2: Error "Variable not found"
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
}
Re: V2: Error "Variable not found"
it means if u intend for thing%A_Index% to declare variables thing1, thing2 through thingn, no its not allowed. ud get an error
Re: V2: Error "Variable not found"
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.
Re: V2: Error "Variable not found"
they werent any more useful than the available alternatives, but they did cause more problem, so they were removed
Re: V2: Error "Variable not found"
this is gonna be a problemAHK_user wrote: ↑21 Aug 2021, 04:01Does 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
}
Re: V2: Error "Variable not found"
writing an automatic v1 -> v2 converter for this case like OP is likely trying to do
Re: V2: Error "Variable not found"
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 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.
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 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.
Re: V2: Error "Variable not found"
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)
Re: V2: Error "Variable not found"
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
}
Re: V2: Error "Variable not found"
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.
#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)