Check multiple vars against single value

Get help with using AutoHotkey and its commands and hotkeys
john_c
Posts: 253
Joined: 05 May 2017, 13:19

Check multiple vars against single value

08 May 2017, 02:50

How I could check multiple vars against single value?

For example, this works fine:

Code: Select all

; Example 1
var1 = value
var2 = value
if (var1 = "value" or var2 = "value") ; This works
    MsgBox, Test
Now I want rewrite it this way:

Code: Select all

; Example 2
var1 = value
var2 = value
if ((var1 or var2) = "value") ; This doesn't work. How to do it ?
    MsgBox, Test
The second example, for some reason, doesn't work. How it could be fixed?
Nightwolf85
Posts: 295
Joined: 05 Feb 2017, 00:03

Re: Check multiple vars against single value

08 May 2017, 09:06

I don't know how to do the format you want, not sure if it is possible in AHK.

But I know why it is not working.
(var1 or var2) evaluates to True(1) or False(0) and those values don't equal "value"
Last edited by Nightwolf85 on 08 May 2017, 09:26, edited 1 time in total.
Rohwedder
Posts: 2116
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Check multiple vars against single value

08 May 2017, 09:22

Hallo,

Code: Select all

var1 = 10
var2 = 10
MsgBox, % (var1|var2)
if ((var1 | var2) = "10") ; This works!
    MsgBox, Test
Nightwolf85
Posts: 295
Joined: 05 Feb 2017, 00:03

Re: Check multiple vars against single value

08 May 2017, 09:27

Rohwedder wrote: if ((var1 | var2) = "10") ; This works!
EDIT*
After testing it only works with numbers, and if the numbers for each variable are the same, if you do this it no longer works:

Code: Select all

var1 = 73
var2 = 25
MsgBox, % (var1 | var2)
if ((var1 | var2) = 73)
    MsgBox, Test
And if you use strings then the | just yields an empty string:

Code: Select all

var1 = "value"
var2 = "value"
MsgBox, % (var1 | var2)
if ((var1 | var2) = "value")
    MsgBox, Test
Last edited by Nightwolf85 on 08 May 2017, 09:47, edited 1 time in total.
Helgef
Posts: 3892
Joined: 17 Jul 2016, 01:02
Contact:

Re: Check multiple vars against single value

08 May 2017, 09:46

If my understanding of the request is correct, you don't want to use |.
| is bitwise-or, || is logical-or. See expressions.
10 | 10 = 10, so you get if (10 = 10), of course it is true, but, 1|10=11, then you get if (11=10) which is false, even though one of the values you wanted to compare was 10.
Maybe try this,

Code: Select all

var1 := "str1"
var2 := "str2"
var3 := "str3"
var4:="str2"
if var4 ~= var1 "|" var2 "|" var3 ; ~= is Shorthand for RegExMatch
	Msgbox, hi
Cheers.
User avatar
jeeswg
Posts: 6648
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Check multiple vars against single value

08 May 2017, 12:00

Some possibilities:

Code: Select all

q:: ;if var in list
list := "str1,str4"
var1 := "str1"
var2 := "str2"

Loop, Parse, list, % ","
{
	needle := A_LoopField

	;assuming that var1/var2 don't contain commas
	if needle in %var1%,%var2%
		MsgBox, % "y"
	else
		MsgBox, % "n"

	;assuming that var1/var2 don't contain characters with a special meaning in RegEx
	;quote: 'the characters \.*?+[{|()^$ must be preceded by a backslash to be seen as literal'
	;remove 'i)' to make it case sensitive
	if RegExMatch(needle, "i)^(" var1 "|" var2 ")$")
		MsgBox, % "y"
	else
		MsgBox, % "n"

	;assuming that var1/var2 don't contain characters with a special meaning in RegEx
	;shorthand for RegExMatch
	if needle ~= "i)^(" var1 "|" var2 ")$"
		MsgBox, % "y"
	else
		MsgBox, % "n"
}
return
I've said before that I think AHK needs something like my JEE_StrContains / JEE_StrIn functions, especially since 'if var contains'/'if var in' will be removed in AHK v2:
Creating a user defined function to search in an Array - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 37&t=30571
Last edited by jeeswg on 08 May 2017, 12:54, edited 1 time in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
noname
Posts: 509
Joined: 19 Nov 2013, 09:15

Re: Check multiple vars against single value

08 May 2017, 12:30

The regex is not fullproof:

Code: Select all

var1 := "str1"
var2 := "str2"
var3 := "str3"
var4:="str1aa"
if var4 ~= var1 "|" var2 "|" var3 ; ~= is Shorthand for RegExMatch
	Msgbox, true
else
	Msgbox, false

;  maybe a variadic function?

if ifanymatch(var4,var1, var2, var3) 
	Msgbox, true
else
	Msgbox, false
	
ifanymatch(ref,par*){
    for k,v in par
     if (v =ref)
     return true
return false
}
soundcloud.com/user-32706894
Helgef
Posts: 3892
Joined: 17 Jul 2016, 01:02
Contact:

Re: Check multiple vars against single value

08 May 2017, 12:40

I'd go with the function approach. :thumbup:
User avatar
jeeswg
Posts: 6648
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Check multiple vars against single value

08 May 2017, 12:51

@noname. Cheers for explaining, I tried to do a foolproof version, in my example above.

RegExMatch(needle, "i)" var1 "|" var2) ;contains a string
RegExMatch(needle, "i)^(" var1 "|" var2 ")$") ;matches a string

[EDIT:] to make var1/var2 literal, see also:
simplest way to make a RegEx needle literal? - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=30420
Last edited by jeeswg on 08 May 2017, 14:58, edited 1 time in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Helgef
Posts: 3892
Joined: 17 Jul 2016, 01:02
Contact:

Re: Check multiple vars against single value

08 May 2017, 14:32

jeeswg wrote:I tried to do a foolproof version, in my example above.
I think one would need to use "\Q \E", but that could occur in the string aswel I suppose.
I'm not going to say it is fool proof, but it is the best I could come up with, you need to decide on a delimiter though, that is d="☕" in this example. ☕ beacuse I'm in that mood.

Code: Select all

d:="☕"
var1 := "str1"
var2 := "str2"
var3 := "str3"
var4:="str3"
;var4:="str3."
if d var4 d ~= "\Q" d var1 d "\E|\Q" d var2 d "\E|\Q" d var3 d "\E" ; ~= is Shorthand for RegExMatch
	Msgbox, true
else
	Msgbox, false
Back to the |, for integers, maybe this,

Code: Select all

a:=0
b:=1
c:=2
d:=3

v:=1 ; compare abcd to v
if (x:=a|b|c|d)|v==x
	msgbox, true
else
	msgbox, false
return
Cheers ☕

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], devnullsp, Google [Bot], kkleinfelter, sikongshan and 146 guests