"AND,OR" for "if var in seq" Topic is solved

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

"AND,OR" for "if var in seq"

07 Jun 2017, 04:45

The "if var in / contains seq" is not support AND/OR.
or I'm doing sth wrong ?

Code: Select all

if var in %foo%
|| var in %bar%
{
	MsgBox Won't hit me "if foo not correct"
}
User avatar
evilC
Posts: 4766
Joined: 27 Feb 2014, 12:30

Re: "AND,OR" for "if var in seq"

07 Jun 2017, 05:17

It's not clear what you are trying to do.
What is the contents of var, foo and bar?

The "if var in" syntax is for checking if something is in a match list, and you do not appear to have provided a list

f var in 1,2,3,5,7,11

According to the docs, if var in %MyItemList% is valid syntax, but I cannot seem to craft a variable MyItemList and have it work.
For example, the following does not work:

Code: Select all

WinGet, list, List
WinGet, hwnd, ID, A
if hwnd in %list%
	msgbox
dsewq1LYJ
Posts: 114
Joined: 26 Aug 2014, 23:21

Re: "AND,OR" for "if var in seq"

07 Jun 2017, 05:31

evilC wrote:It's not clear what you are trying to do.
What is the contents of var, foo and bar?

The "if var in" syntax is for checking if something is in a match list, and you do not appear to have provided a list

f var in 1,2,3,5,7,11

According to the docs, if var in %MyItemList% is valid syntax, but I cannot seem to craft a variable MyItemList and have it work.
For example, the following does not work:

Code: Select all

WinGet, list, List
WinGet, hwnd, ID, A
if hwnd in %list%
	msgbox
Sorry my bad.
Let me give you a full example.

Code: Select all

foo:="1,2,3"
bar:="Hwnd,Qt5,Window"
WinGetClass,C,A
if C in %foo%
    doFoo()
else if C contains %bar%
    doFoo()
You can't use OR here to combine these two if statements.

Code: Select all

foo:="1,2,3"
bar:="Hwnd,Qt5,Window"
WinGetClass,C,A
if C in %foo% ; Only work if the "if C in %foo%", won't hit doFoo() when "C contains %bar% is true"
|| C contains %bar%
    doFoo()
User avatar
Masonjar13
Posts: 1509
Joined: 20 Jul 2014, 10:16
GitHub: Masonjar13
Location: Не Россия

Re: "AND,OR" for "if var in seq"  Topic is solved

07 Jun 2017, 20:22

"If var in" doesn't support or/and. You can, however, append the two to act as an or. Otherwise, you will need a separate statement.

Code: Select all

foo:="1,2,3"
bar:="Hwnd,Qt5,Window"

;ex1
WinGetClass,C,A
if C in %foo%
    doFoo()
else if C contains %bar%
    doFoo()
You can also create functions for these statements, allowing you to use expression syntax, which is what I do.

Code: Select all

foo:="1,2,3"
bar:="Hwnd,Qt5,Window"

winGetClass,C,A
if(ifIn(C,foo)||ifContains(C,bar))
    doFoo()



ifContains(haystack,needle){
    if haystack contains %needle%
        return 1
    return 0
}
ifIn(needle,haystack){
    if needle in %haystack%
        return 1
    return 0
}
OS: Windows 10 Pro | Editor: Notepad++
My Personal Function Library | Old Build - New Build
User avatar
Exaskryz
Posts: 2876
Joined: 17 Oct 2015, 20:28

Re: "AND,OR" for "if var in seq"

07 Jun 2017, 21:44

evilC wrote:It's not clear what you are trying to do.
What is the contents of var, foo and bar?

The "if var in" syntax is for checking if something is in a match list, and you do not appear to have provided a list

f var in 1,2,3,5,7,11

According to the docs, if var in %MyItemList% is valid syntax, but I cannot seem to craft a variable MyItemList and have it work.
For example, the following does not work:

Code: Select all

WinGet, list, List
WinGet, hwnd, ID, A
if hwnd in %list%
	msgbox
I imagine that code does not work because list returns a pseudo-array. list1, list2, list3, etc. You can try this, which in a quick test does produce the msgbox:

Code: Select all

matchlist:=""
WinGet, list, List
Loop % list
matchlist.=list%A_Index% ","
StringTrimRight, matchlist, matchlist, 1 ; remove the final ","
WinGet, hwnd, ID, A
if hwnd in %matchlist%
   MsgBox hi
return
dsewq1LYJ
Posts: 114
Joined: 26 Aug 2014, 23:21

Re: "AND,OR" for "if var in seq"

08 Jun 2017, 01:10

Masonjar13 wrote:"If var in" doesn't support or/and. You can, however, append the two to act as an or. Otherwise, you will need a separate statement.

Code: Select all

foo:="1,2,3"
bar:="Hwnd,Qt5,Window"

;ex1
WinGetClass,C,A
if C in %foo%
    doFoo()
else if C contains %bar%
    doFoo()
You can also create functions for these statements, allowing you to use expression syntax, which is what I do.

Code: Select all

foo:="1,2,3"
bar:="Hwnd,Qt5,Window"

winGetClass,C,A
if(ifIn(C,foo)||ifContains(C,bar))
    doFoo()



ifContains(haystack,needle){
    if haystack contains %needle%
        return 1
    return 0
}
ifIn(needle,haystack){
    if needle in %haystack%
        return 1
    return 0
}
Using function in AHK in not good IMO.

it would slow down the script (Tiny performance drop).

Took 0.196610

Code: Select all

Loop 100000 ; 100K call
{
    WinGetTitle,T,A
    if ifIn(T,"qwe,asd,zxc,wer,sdf,xcv,ert,tyu,ghj,bnm")
    || ifContains(T,"Vim")
    {}
}
Took 0.114200

Code: Select all

Loop 100000 ; 100K call
{
    WinGetTitle,T,A
    if T in qwe,asd,zxc,wer,sdf,xcv,ert,tyu,ghj,bnm
    {}
    else if T contains Vim
    {}
}
but it's fine, I'll keep wait until AHK newer version.
or just it can not eaily implement in C ...
Last edited by dsewq1LYJ on 08 Jun 2017, 01:14, edited 1 time in total.
dsewq1LYJ
Posts: 114
Joined: 26 Aug 2014, 23:21

Re: "AND,OR" for "if var in seq"

08 Jun 2017, 01:13

Exaskryz wrote:
evilC wrote:It's not clear what you are trying to do.
What is the contents of var, foo and bar?

The "if var in" syntax is for checking if something is in a match list, and you do not appear to have provided a list

f var in 1,2,3,5,7,11

According to the docs, if var in %MyItemList% is valid syntax, but I cannot seem to craft a variable MyItemList and have it work.
For example, the following does not work:

Code: Select all

WinGet, list, List
WinGet, hwnd, ID, A
if hwnd in %list%
	msgbox
I imagine that code does not work because list returns a pseudo-array. list1, list2, list3, etc. You can try this, which in a quick test does produce the msgbox:

Code: Select all

matchlist:=""
WinGet, list, List
Loop % list
matchlist.=list%A_Index% ","
StringTrimRight, matchlist, matchlist, 1 ; remove the final ","
WinGet, hwnd, ID, A
if hwnd in %matchlist%
   MsgBox hi
return
Sorry for misunderstand.

I try to combine two if statements for "if var in %foo%" AND "if var in %bar%" by AND or OR
but as Masonjar13 says, AND or OR is not support these two syntaxs right now.

Thanks for sharing and help !
User avatar
Masonjar13
Posts: 1509
Joined: 20 Jul 2014, 10:16
GitHub: Masonjar13
Location: Не Россия

Re: "AND,OR" for "if var in seq"

08 Jun 2017, 02:00

The performance difference is negligible at best. I'd generally recommend use of functions due to versatility and ease of managing/updating. But you might also try using RegExMatch(). Note that if you use the $ anchor, it will return 0 if there is a match. The ~= operator acts as a shorthand method of RegExMatch().
OS: Windows 10 Pro | Editor: Notepad++
My Personal Function Library | Old Build - New Build
aaffe
Posts: 158
Joined: 16 Jan 2014, 04:23

Re: "AND,OR" for "if var in seq"

08 Jun 2017, 05:49

Code: Select all

foo:="1,2,3"
bar:="Hwnd,Qt5,Window"

WinGetClass,C,A
full:=foo . "," . bar
if C in %full%
    doFoo()

Return to “Ask For Help”

Who is online

Users browsing this forum: boiler, Google [Bot], Odlanir and 124 guests