if var in MatchList

Discuss the future of the AutoHotkey language
guest3456
Posts: 2555
Joined: 09 Oct 2013, 10:31

if var in MatchList

02 Sep 2016, 13:29

v2-changes says that in is reserved as an operator for future use in expressions

it also said that this if var in MatchList command was removed. was there any replacement for it? or do we just have to write it ourselves with Loop, Parse?

Code: Select all

IsItemInList(item, list, delim="")
{
   delim := (delim = "") ? "," : delim
   Loop, Parse, list, %delim%
   {
      if (A_LoopField = item)
         return true
   }
   return false
}


HotKeyIt
Posts: 1866
Joined: 29 Sep 2013, 18:35
Contact:

Re: if var in MatchList

02 Sep 2016, 14:17

Afaik InStr is the fastest:

Code: Select all

IsItemInList(item, list, del:=",")
{
	If IsObject(list){
		for k,v in list
			if (v=item)
				return true
		return false
	} else Return !!InStr(del list del, del item del)
}

MsgBox % IsItemInList("ahk","btw,ahk")
EDIT:
Changed to check array/object directly.
lexikos
Posts: 6488
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: if var in MatchList

02 Sep 2016, 20:17

was there any replacement for it?
No. That's why in is reserved for future use.
or do we just have to write it ourselves with Loop, Parse?
No. You can copy someone else or use some other method if you want. :P

It probably wouldn't be difficult for someone to reuse the if-var-in code to implement the in operator. I didn't do it because I think the in operator should have other capabilities, like searching in an array, but haven't been interested enough to plan it out properly.
guest3456
Posts: 2555
Joined: 09 Oct 2013, 10:31

Re: if var in MatchList

22 Sep 2016, 01:35

for reference, another possible one-liner alternative:

Code: Select all

if var in exe,bat,com
    MsgBox The file extension is an executable type.

if RegExMatch(var, "^exe$|^bat$|^com$")
    MsgBox The file extension is an executable type.

User avatar
jNizM
Posts: 2453
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: if var in MatchList

22 Sep 2016, 02:05

Code: Select all

if (var ~= "exe|bat|com")
[AHK] 1.1.30.03 x64 Unicode | [WIN] 10 Pro (Version 1903) x64 | [GitHub] Profile
Donations are appreciated if I could help you
lexikos
Posts: 6488
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: if var in MatchList

22 Sep 2016, 02:59

guest3456's regex is case-sensitive.

jNizM's regex is case-sensitive, but otherwise behaves like if var contains exe,bat,com because of the lack of ^anchors$.

Code: Select all

if var ~= "^(?i:exe|bat|com)$"
guest3456
Posts: 2555
Joined: 09 Oct 2013, 10:31

Re: if var in MatchList

22 Sep 2016, 09:24

lexikos wrote:guest3456's regex is case-sensitive.

jNizM's regex is case-sensitive, but otherwise behaves like if var contains exe,bat,com because of the lack of ^anchors$.

Code: Select all

if var ~= "^(?i:exe|bat|com)$"
can you explain that (?i: syntax? i'm not seeing it in the regex quickref. i would've thought to put the 'i' at the start: i)^(exe|bat|com)$

lexikos
Posts: 6488
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: if var in MatchList

22 Sep 2016, 20:22

Look to the bottom of the quick reference.
Final note: Although this page touches upon most of the commonly-used RegEx features, there are quite a few other features you may want to explore such as conditional subpatterns. The complete PCRE manual is at www.pcre.org/pcre.txt
Drugoy
Posts: 48
Joined: 11 Jun 2016, 07:37
GitHub: Drugoy

Re: if var in MatchList

17 Oct 2016, 07:49

lexikos wrote:
was there any replacement for it?
No. That's why in is reserved for future use.
or do we just have to write it ourselves with Loop, Parse?
No. You can copy someone else or use some other method if you want. :P

It probably wouldn't be difficult for someone to reuse the if-var-in code to implement the in operator. I didn't do it because I think the in operator should have other capabilities, like searching in an array, but haven't been interested enough to plan it out properly.
I use if var in MatchList quite often. I would love if it also worked with arrays.
I feel like you don't add some things or cut some things away in order to make ahk's stdlib (I guess, it's right in the ahk.exe itself) as small, as possible.
I wish there was an ahk branch with a rich stdlib.
lexikos
Posts: 6488
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: if var in MatchList

17 Oct 2016, 23:41

The term "stdlib" is used for functions which exist as separate scripts in a Lib directory but can be automatically included.

This has nothing to do with keeping AutoHotkey small. If I was planning to NOT implement it, there would be no need to reserve the word. It will be implemented, but may be totally different to how it was in v1. The point of reserving it is to ensure it can be added later, if it doesn't make the cut for v2.0.
guest3456
Posts: 2555
Joined: 09 Oct 2013, 10:31

Re: if var in MatchList

15 Nov 2016, 19:47

lexikos wrote:guest3456's regex is case-sensitive.

jNizM's regex is case-sensitive, but otherwise behaves like if var contains exe,bat,com because of the lack of ^anchors$.

Code: Select all

if var ~= "^(?i:exe|bat|com)$"
how would you use the regex operator for if not in?

negative lookahead isn't working properly for me. am i doing it wrong?

Code: Select all

if (var~= "(?!^(?i:exe|bat|com)$).*")

lexikos
Posts: 6488
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: if var in MatchList

15 Nov 2016, 22:13

Just add the "not" operator... (and parentheses).

Your pattern will match at the first position which does not immediately precede the sub pattern, such as position 2, because the ^ assertion is inside the look ahead.

Return to “AutoHotkey v2 Development”

Who is online

Users browsing this forum: No registered users and 5 guests