(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)

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] [Download] GeSHi © Codebox Plus

IF (wParam = 0x6E or wParam = 0xBE or wParam = 0xBC)
ToolTip


I've tried other ways like:

Code: [Select all] [Download] GeSHi © Codebox Plus

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: 2100
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] [Expand] [Download] GeSHi © Codebox Plus

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] [Download] GeSHi © Codebox Plus

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

If you don't care about performance.
User avatar
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)

16 Sep 2018, 12:13

Guest wrote:

Code: [Select all] [Download] GeSHi © Codebox Plus

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)

16 Sep 2018, 13:21

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)

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] [Download] GeSHi © Codebox Plus

if Var contains Item1,Item2,Item3


How do you do this with Expressions like RegExMatch shortcuts?

Code: [Select all] [Download] GeSHi © Codebox Plus

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: 4963
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] [Expand] [Download] GeSHi © Codebox Plus

DRocks
Posts: 89
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] [Download] GeSHi © Codebox Plus

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: 4963
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] [Download] GeSHi © Codebox Plus

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] [Download] GeSHi © Codebox Plus

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.
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)

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: 89
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

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], NovaRanger and 87 guests