Error if ARRAY[i] in LIST OR if DICT[k][i] in LIST

Get help with using AutoHotkey and its commands and hotkeys
dsewq1LYJ
Posts: 114
Joined: 26 Aug 2014, 23:21

Error if ARRAY[i] in LIST OR if DICT[k][i] in LIST

07 Sep 2017, 01:04

Issue:

Code: Select all

if Lorem_dict[__ITR__][1] in %str_exe_Lorem_%
Error:
Error: The following variable name contains an illegal character.
Indirectly fix:

Code: Select all

P := Lorem_dict[__ITR__][1]
if P in %str_exe_Lorem_%

COPY AND PASTE ME TO REVIEW PROBLEM

Code: Select all

; https://autohotkey.com/docs/commands/IfIn.htm
AN_INT := 15
AN_ARRAY       := [1, 1, 2, 3, 5, 8, 15]

STR_IF_IN_     := "0,4,6,9,15"
STR_IF_NOT_IN_ := "0,4,6,9,15"
if AN_INT in %STR_IF_IN_%
    MsgBox % "NO ERROR"

if AN_ARRAY[7] in STR_IF_IN_
    MsgBox % "PASS"
if AN_ARRAY[7] in STR_IF_NOT_IN_
    MsgBox % "PASS BUT WRONG"
 ; NOTE: UNCOMMENT ME
; if AN_ARRAY[7] in %STR_IF_IN_%
    ; MsgBox % "ERROR"
 ; NOTE: UNCOMMENT ME
; if AN_ARRAY[7] in %STR_IF_NOT_IN_%
    ; MsgBox % "ERROR"

A_DICT := { "alpha"   : "lorem"
          , "beta"    : "ipsum"
          , "gamma"   : "dolor"
          , "delta"   : "sit"
          , "epsilon" : "amet" }
STR_IF_IN_     := "lorem,ipsum,dolor,sit,amet"
STR_IF_NOT_IN_ := "hello,world,auto,hot,key"
if A_DICT["alpha"] in STR_IF_IN_
    MsgBox % "PASS"
if A_DICT["beta"] in STR_IF_NOT_IN_
    MsgBox % "PASS BUT WRONG"
 ; NOTE: UNCOMMENT ME
; if A_DICT["alpha"] in %STR_IF_IN_%
    ; MsgBox % "ERROR"
 ; NOTE: UNCOMMENT ME
; if A_DICT["beta"] in %STR_IF_NOT_IN_%
    ; MsgBox % "ERROR"

Helgef
Posts: 4409
Joined: 17 Jul 2016, 01:02
Contact:

Re: Error if ARRAY[i] in LIST OR if DICT[k][i] in LIST

07 Sep 2017, 02:03

if in wrote:Var - The name of the variable whose contents will be checked.
source
array is not a variable name, it is an expression. Hence if array in list an expression style if, where the content of array is concatenated with the blank variable in and the variable list. If you set array!=0 and list!=0 the expression is !false, i.e., it is true.
Example,

Code: Select all

a:=[0]
list:=0
in:=0
if a[1] in list
	msgbox yes
else 
	msgbox no
	
a:=[0]
list:=0
in:=1
if a[1] in list
	msgbox yes
else 
	msgbox no
When you do %STR_IF_IN_%, you are doing a double deref, since the contents of STR_IF_IN_ (0,4,6,9,15) is not a vaild variable name, you get an error.
Example of valid double deref,

Code: Select all

a:=[0]
list:="b" ; change to eg, "0,1" and you get an error
b:=true
in:=1
if a[1] in %list%
	msgbox yes
else 
	msgbox no
Cheers.
dsewq1LYJ
Posts: 114
Joined: 26 Aug 2014, 23:21

Re: Error if ARRAY[i] in LIST OR if DICT[k][i] in LIST

07 Sep 2017, 08:50

Helgef wrote:
if in wrote:Var - The name of the variable whose contents will be checked.
source
array is not a variable name, it is an expression. Hence if array in list an expression style if, where the content of array is concatenated with the blank variable in and the variable list. If you set array!=0 and list!=0 the expression is !false, i.e., it is true.
Example,

Code: Select all

a:=[0]
list:=0
in:=0
if a[1] in list
	msgbox yes
else 
	msgbox no
	
a:=[0]
list:=0
in:=1
if a[1] in list
	msgbox yes
else 
	msgbox no
When you do %STR_IF_IN_%, you are doing a double deref, since the contents of STR_IF_IN_ (0,4,6,9,15) is not a vaild variable name, you get an error.
Example of valid double deref,

Code: Select all

a:=[0]
list:="b" ; change to eg, "0,1" and you get an error
b:=true
in:=1
if a[1] in %list%
	msgbox yes
else 
	msgbox no
Cheers.
Thanks !

I think my mind just stuck when I met this problem :headwall:
That's not a glitch, I knew how to solve this now :beard:

Return to “Ask For Help”

Who is online

Users browsing this forum: Baby Flurry Heart, Bing [Bot], Google [Bot] and 148 guests