Page 1 of 1

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

Posted: 07 Jun 2017, 04:45
by dsewq1LYJ
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"
}

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

Posted: 07 Jun 2017, 05:17
by evilC
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

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

Posted: 07 Jun 2017, 05:31
by dsewq1LYJ
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()

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

Posted: 07 Jun 2017, 13:44
by evilC

Code: Select all

foo := "1,2,3"
bar := 1

if bar in %foo%
	msgbox
or

Code: Select all

foo = 1,2,3
bar = 1

if bar in %foo%
	msgbox

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

Posted: 07 Jun 2017, 20:22
by Masonjar13
"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
}

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

Posted: 07 Jun 2017, 21:44
by Exaskryz
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

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

Posted: 08 Jun 2017, 01:10
by dsewq1LYJ
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 ...

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

Posted: 08 Jun 2017, 01:13
by dsewq1LYJ
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 !

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

Posted: 08 Jun 2017, 02:00
by Masonjar13
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().

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

Posted: 08 Jun 2017, 05:49
by aaffe

Code: Select all

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

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