e.g.:
Code: Select all
if var in/contains ['foo', 'bar', 'others']
OR if var in/contains 'foo,bar,others'
- Two consecutive commas results in a single literal comma
- To include a blank item in the list, make the first character a comma
Code: Select all
if var in/contains ['foo', 'bar', 'others']
OR if var in/contains 'foo,bar,others'
Code: Select all
matchlist := "abc|def|ghi|"
needle := "def"
if (SubStr(matchlist, i:=InStr(matchlist, needle), InStr(matchlist, "|",, i-1)-1) == needle)
MsgBox Match found
Yes, but AutoHotkey internally takes care of distinguishing the delimiter and members.toralf wrote:Var and matchlist are 'single' strings as well in the if commands
Code: Select all
if {'abc':1, 'def':1, 'ghi':1}['def']
MsgBox Match found
Code: Select all
Var = Tuesday
Matchlist1 = Monday,Tuesday,Wednesday
Matchlist2 = Thursday,Friday,Saturday
If Var in %Matchlist1%
MsgBox 1) Right: %Var% is in %Matchlist1%
Else
MsgBox 1) Wrong: %Var% is not in %Matchlist1%
If InStr(Matchlist1, Var)
MsgBox 2) Right: %Var% is in %Matchlist1%
Else
MsgBox 2) Wrong: %Var% is not in %Matchlist1%
If Var not in %Matchlist2%
MsgBox 3) Right: %Var% is not in %Matchlist2%
Else
MsgBox 3) Wrong: %Var% is in %Matchlist2%
If !InStr(Matchlist2, Var)
MsgBox 4) Right: %Var% is not in %Matchlist2%
Else
MsgBox 4) Wrong: %Var% is in %Matchlist2%
Var = Tuesday
Matchlist1 = dusk,night,day
Matchlist2 = hour,minute,second
If Var contains %Matchlist1%
MsgBox 5) Right: %Var% contains %Matchlist1%
Else
MsgBox 5) Wrong: %Var% does not contain %Matchlist1%
; If (Pos := InStr(Var, "(dusk|night|day)")) ;is always wrong (not finding the substring)
; If (Pos := RegExMatch(Var, "(dusk|night|day)"))
If Var ~= "(dusk|night|day)"
MsgBox 6) Right: %Var% contains %Matchlist1% at %Pos%
Else
MsgBox 6) Wrong: %Var% does not contain %Matchlist1% at %Pos%
If Var not contains %Matchlist2%
MsgBox 7) Right: %Var% does not contain %Matchlist2%
Else
MsgBox 7) Wrong: %Var% contains %Matchlist2%
; If !(Pos := InStr(Var, "(hour|minute|second)")) ;is always right (not finding the substring), but with false positives
; If !(Pos := RegExMatch(Var, "(hour|minute|second)"))
If !(Var ~= "(hour|minute|second)")
MsgBox 8) Right: %Var% not contains %Matchlist2% at %Pos%
Else
MsgBox 8) Wrong: %Var% contains %Matchlist2% at %Pos%
Yep, the code is for if var in only. Regardless, the built in if var in/contains still beats the workarounds both in performance and readability...toralf wrote:Your code didn't really duplicate If Var contains result either.
Return to “AutoHotkey Development”
Users browsing this forum: No registered users and 42 guests