(solved) If-Or Multi Match Syntax? if (x=1 OR x=2 OR x=3) Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
DRocks
Posts: 565
Joined: 08 May 2018, 10:20

(solved) If-Or Multi Match Syntax? if (x=1 OR x=2 OR x=3)

16 Sep 2018, 09:56

Hi guys

I've been struggling with this question since the begining of using AHK and I just can't spot the information that will clear this out.

I want to use an IF OR condition and not repeat ten times the same variable to evaluate.
How do you abreviate this example:

Code: Select all

IF (wParam = 0x6E or wParam = 0xBE or wParam = 0xBC)
			ToolTip
I've tried other ways like:

Code: Select all

IF (wParam = 0x6E or 0xBE or 0xBC) ; wrong all messages validate true when its false
IF (wParam = 0x6E, 0xBE, 0xBC) ; wrong
IF (wParam = 0x6E | 0xBE | 0xBC) ; wrong
IF (wParam = 0x6E 0xBE 0xBC) ; wrong
Please tell me how to not repeat the var every time it is redundant and sometimes you can load a line abusively :)

thanks for any tips
Alex :mrgreen:
Last edited by DRocks on 17 Sep 2018, 05:20, edited 2 times in total.
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: If-Or Syntax? if (such=1 OR such=2 OR such=3)

16 Sep 2018, 10:37

try inRange()
example:

Code: Select all



;-------------------------------------------------------------------------------
WM_KEYDOWN(wParam, lParam, msg, hwnd) { ;
;-------------------------------------------------------------------------------
    ; we have a key
    Key := GetKeyVK("vk" wParam)
    MsgBox, inRange(Key,  72,  87)  ; [0..9]
}


;-------------------------------------------------------------------------------
inRange(Value, Lower, Upper) { ; boolean
;-------------------------------------------------------------------------------
    Return, Value >= Min(Lower, Upper)
         && Value <= Max(Lower, Upper)
}
I hope that helps, there are also other options.
Guest

Re: If-Or Syntax? if (such=1 OR such=2 OR such=3)

16 Sep 2018, 11:48

Code: Select all

okvalues := [0x11, 0x22]
if (okvalues.haskey(wparam))
If you don't care about performance.
User avatar
Nextron
Posts: 1391
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win10 AHK: Unicode x32

Re: If-Or Syntax? if (such=1 OR such=2 OR such=3)

16 Sep 2018, 12:13

Guest wrote:

Code: Select all

okvalues := [0x11, 0x22]
if (okvalues.haskey(wparam))
If you don't care about performance.
The same thought occurred to me, but you're setting the values and then checking for keys, which doesn't function like you want. Instead you'd end up having to do: okvalues := {0x11:"", 0x22:""} which is less clean and doesn't allow every arbitrary key string.
DRocks
Posts: 565
Joined: 08 May 2018, 10:20

Re: If-Or Syntax? if (such=1 OR such=2 OR such=3)

16 Sep 2018, 13:21

thanks to all of you guys I'm checking this out
DRocks
Posts: 565
Joined: 08 May 2018, 10:20

Re: If-Or Syntax? if (such=1 OR such=2 OR such=3)

16 Sep 2018, 18:03

Ok so to follow up I think its got a bit more complex than what I intended to communicate.

In others words I'm wondering how to NOT have to repeat the Variable's name when you want to check it for multiple options.

For example the Deprecated method is the closest to what I'm looking for:

Code: Select all

if Var contains Item1,Item2,Item3
How do you do this with Expressions like RegExMatch shortcuts?

Code: Select all

if (Var ~= item1 or item2 or item3)
As the example show we are always evaluating the same variable looking for one of these condition to be true.
Why does it require to repeat the name of the variable and the ~= for EACH possibility?
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: If-Or Syntax? if (such=1 OR such=2 OR such=3)  Topic is solved

16 Sep 2018, 18:17

Here's a way. Cheers.

Code: Select all

q:: ;compare hex values
wParam := 0
MsgBox, % (Format("0x{:X}", wParam) ~= "^(0x6E|0xBE|0xBC)$")
wParam := 0x6E
MsgBox, % (Format("0x{:X}", wParam) ~= "^(0x6E|0xBE|0xBC)$")
wParam := 0x6E+0
MsgBox, % (Format("0x{:X}", wParam) ~= "^(0x6E|0xBE|0xBC)$")
wParam := "0x6E"
MsgBox, % (Format("0x{:X}", wParam) ~= "^(0x6E|0xBE|0xBC)$")
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
DRocks
Posts: 565
Joined: 08 May 2018, 10:20

Re: If-Or Syntax? if (such=1 OR such=2 OR such=3)

17 Sep 2018, 05:16

Thank you!

So as I understand it, you use Format just to make sure that the RegExMatch will not be searching a faulty string when we are dealing with wParam.
But in more simple cases I can get rid of Format and just go straigth like the last example where I added "heythere" as an haystack

Code: Select all

wParam := "0xE1"
MsgBox, % (Format("0x{:X}", wParam) ~= "^(0x6E|0xBE|0xBC|0xE1|heythere)$")
wParam := "heythere"
MsgBox, % "wParam :" wParam .=  wParam ~= "^(0x6E|0xBE|0xBC|0xE1|heythere)$"
return
Great tip and I also just begin to understand the use of Format, it is so hard to understand for me. The doc gets me confused more than helps.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: (solved) If-Or Multi Match Syntax? if (x=1 OR x=2 OR x=3)

17 Sep 2018, 05:27

- Re. 'heythere', I suppose so. You could do:

Code: Select all

MsgBox, % (wParam = "heythere") || (Format("0x{:X}", wParam) ~= "^(0x6E|0xBE|0xBC|0xE1)$")
- The difficulty is this: checking a string against a string is simple enough, but the form of a number can vary, e.g. it might be '0xF or '15'. So we ensure it's in the right format for a string comparison.
- Converting the number to decimal, and then comparing that via 'in' against decimal numbers, is a possibility, but then you lose the hex formatted numbers:

Code: Select all

wParam += 0
if wParam in 110,190,225
- I have various FORMAT FUNCTION EXAMPLES here:
jeeswg's characters tutorial - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=26486
- Yes I think it's quite hard to understand also, I would like the documentation to have more examples.
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: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: (solved) If-Or Multi Match Syntax? if (x=1 OR x=2 OR x=3)

17 Sep 2018, 09:23

I'd use if var in matchlist over ~=. Alternative, a variadic function plus for loop, it is much simpler., avoids strings. In / contains are not Deprecated.

Cheers.
DRocks
Posts: 565
Joined: 08 May 2018, 10:20

Re: (solved) If-Or Multi Match Syntax? if (x=1 OR x=2 OR x=3)

17 Sep 2018, 17:21

Thanks Helgef!

Can you tell me why you would go for if var in matchlist? is it faster than RegExMatch performance wise?
Interesting tip for Variadic function.. I don't have info about this yet. I hope its clear in the docs.

Thank you
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: (solved) If-Or Multi Match Syntax? if (x=1 OR x=2 OR x=3)

21 Sep 2018, 02:39

I would go for matchlist over ~= for readbility and maintainability, the latter looks like a joke imo. Matchlist might be faster, but in most cases, insignificant (guess). Function example, superior for maintainability, readability and convenience,

Code: Select all

varIsInList(var, list*){
	for k, item in list
		if ( var = item )
			return true
	return false
}
; example
wParam := 0xA
if varIsInList(wParam, 0x1, 0x2, 0xa)
	msgbox yes

if varIsInList(wParam, 0x1, 0x2, 0xb)
	msgbox yes
else 
	msgbox no
Cheers.
DRocks
Posts: 565
Joined: 08 May 2018, 10:20

Re: (solved) If-Or Multi Match Syntax? if (x=1 OR x=2 OR x=3)

21 Sep 2018, 05:31

hmm!! pretty interesting man. thanks alot. I'll try this Helgef :)

But so far just to help further readers, the regexmatch short is also working.
if (var ~= ("help|me|find|my|words")
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: (solved) If-Or Multi Match Syntax? if (x=1 OR x=2 OR x=3)

21 Sep 2018, 05:49

you didn't pay attention to jeeswg's example, that will match, eg, var := "helpX". To note, if you like to use ~= or regexmatch, you can prefix the needle with i), to make it case insensitive.

Cheers.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 157 guests