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

Get help with using AutoHotkey and its commands and hotkeys
DRocks
Posts: 89
Joined: 08 May 2018, 10:20

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

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:
`IF (wParam = 0x6E or wParam = 0xBE or wParam = 0xBC) ToolTip`

I've tried other ways like:
`IF (wParam = 0x6E or 0xBE or 0xBC) ; wrong all messages validate true when its falseIF (wParam = 0x6E, 0xBE, 0xBC) ; wrongIF (wParam = 0x6E | 0xBE | 0xBC) ; wrongIF (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
Last edited by DRocks on 17 Sep 2018, 05:20, edited 2 times in total.
wolf_II
Posts: 2100
Joined: 08 Feb 2015, 20:55

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

try inRange()
example:
I hope that helps, there are also other options.
Guest

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

`okvalues := [0x11, 0x22]if (okvalues.haskey(wparam))`

If you don't care about performance.
Nextron
Posts: 1208
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win7 x64 AHK: Unicode x32

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

Guest wrote:
`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: 89
Joined: 08 May 2018, 10:20

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

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

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

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:
`if Var contains Item1,Item2,Item3`

How do you do this with Expressions like RegExMatch shortcuts?
`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?
jeeswg
Posts: 4964
Joined: 19 Dec 2016, 01:58
Location: UK

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

Here's a way. Cheers.
DRocks
Posts: 89
Joined: 08 May 2018, 10:20

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

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

`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.
jeeswg
Posts: 4964
Joined: 19 Dec 2016, 01:58
Location: UK

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

- Re. 'heythere', I suppose so. You could do:
`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:
`wParam += 0if 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.
Helgef
Posts: 3151
Joined: 17 Jul 2016, 01:02
Contact:

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

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: 89
Joined: 08 May 2018, 10:20

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

Thanks Helgef!

Can you tell me why you would go for if var in matchlist? is it faster than RegExMatch performance wise?