In and Contains plans...

Discuss the future of the AutoHotkey language
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
GitHub: cocobelgica

In and Contains plans...

01 Jul 2014, 11:28

These are currently reserved keywords. I'm just wondering on what are the plans for these in terms of syntax/usage...
e.g.:

Code: Select all

if var in/contains ['foo', 'bar', 'others']
OR if var in/contains 'foo,bar,others'
I prefer the former as it's clearer. Hence no need for:
  • Two consecutive commas results in a single literal comma
  • To include a blank item in the list, make the first character a comma
Also in can become multipurpose, e.g.: if key in {a:'A', b:'B', c:'C'}
lexikos
Posts: 6668
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: In and Contains plans...

01 Jul 2014, 20:51

It's up in the air.
User avatar
joedf
Posts: 7352
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada
Contact:

Re: In and Contains plans...

01 Jul 2014, 21:02

Interesting idea...
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500 @ 4.00 GHz, 2x8GB G.Skill RipJaws V - DDR4 3280 MHz, NVIDIA GTX 1060 6GB | [About Me] | [ASPDM - StdLib Distribution]
[Populate the AHK MiniCity!] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library] | [About the AHK Foundation]
toralf
Posts: 642
Joined: 27 Apr 2014, 21:08
Location: Germany

Re: In and Contains plans...

01 Jul 2014, 21:41

Is there actually any benefit over Regexmatch, instr() or ~=?
Besides that it is easier human readable?
ciao
toralf
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
GitHub: cocobelgica

Re: In and Contains plans...

02 Jul 2014, 01:55

@toralf InStr() operates on a single string while in/contains operates on a MatchList. As for RegExmatch or ~=, under v1.1, in/contains is way way way faster.
toralf
Posts: 642
Joined: 27 Apr 2014, 21:08
Location: Germany

Re: In and Contains plans...

02 Jul 2014, 02:29

IMHO instr() can be written to fulfill the same. Basically it is a haystack and needle. Thus, depending on if 'in' or 'contains' is required one or the other is the haystack. After looking up the command I'm sure For 'in'. For 'contain' I would have to test it.
Var and matchlist are 'single' strings as well in the if commands
ciao
toralf
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
GitHub: cocobelgica

Re: In and Contains plans...

02 Jul 2014, 03:30

Something like this which requires 3 function calls:

Code: Select all

matchlist := "abc|def|ghi|"
needle := "def"
if (SubStr(matchlist, i:=InStr(matchlist, needle), InStr(matchlist, "|",, i-1)-1) == needle)
	MsgBox Match found
toralf wrote:Var and matchlist are 'single' strings as well in the if commands
Yes, but AutoHotkey internally takes care of distinguishing the delimiter and members.
The current substitute I use for v2 is (also the fastest, based on some QPX() tests):

Code: Select all

if {'abc':1, 'def':1, 'ghi':1}['def']
	MsgBox Match found
Limitation, you can't do case-sensitive check(s).
toralf
Posts: 642
Joined: 27 Apr 2014, 21:08
Location: Germany

Re: In and Contains plans...

02 Jul 2014, 16:25

I was able to use InStr() for If Var in, but not for If Var contains. There I had to use RegExMatch() or ~=.
Your code didn't really duplicate If Var contains result either.

Code: Select all

Var = Tuesday
Matchlist1 = Monday,Tuesday,Wednesday
Matchlist2 = Thursday,Friday,Saturday

If Var in %Matchlist1%
  MsgBox 1) Right: %Var% is in %Matchlist1%
Else
  MsgBox 1) Wrong: %Var% is not in %Matchlist1%

If InStr(Matchlist1, Var)
  MsgBox 2) Right: %Var% is in %Matchlist1%
Else
  MsgBox 2) Wrong: %Var% is not in %Matchlist1%

If Var not in %Matchlist2%
  MsgBox 3) Right: %Var% is not in %Matchlist2%
Else
  MsgBox 3) Wrong: %Var% is in %Matchlist2%

If !InStr(Matchlist2, Var)
  MsgBox 4) Right: %Var% is not in %Matchlist2%
Else
  MsgBox 4) Wrong: %Var% is in %Matchlist2%

Var = Tuesday
Matchlist1 = dusk,night,day
Matchlist2 = hour,minute,second

If Var contains %Matchlist1%
  MsgBox 5) Right: %Var% contains %Matchlist1%
Else
  MsgBox 5) Wrong: %Var% does not contain %Matchlist1%

; If (Pos := InStr(Var, "(dusk|night|day)"))      ;is always wrong (not finding the substring)
; If (Pos := RegExMatch(Var, "(dusk|night|day)"))
If Var ~= "(dusk|night|day)"
  MsgBox 6) Right: %Var% contains %Matchlist1% at %Pos%
Else
  MsgBox 6) Wrong: %Var% does not contain %Matchlist1% at %Pos%

If Var not contains %Matchlist2%
  MsgBox 7) Right: %Var% does not contain %Matchlist2%
Else
  MsgBox 7) Wrong: %Var% contains %Matchlist2%

; If !(Pos := InStr(Var, "(hour|minute|second)"))   ;is always right (not finding the substring), but with false positives
; If !(Pos := RegExMatch(Var, "(hour|minute|second)"))
If !(Var ~= "(hour|minute|second)")
  MsgBox 8) Right: %Var% not contains %Matchlist2% at %Pos%
Else
  MsgBox 8) Wrong: %Var% contains %Matchlist2% at %Pos%
ciao
toralf
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
GitHub: cocobelgica

Re: In and Contains plans...

03 Jul 2014, 03:53

toralf wrote:Your code didn't really duplicate If Var contains result either.
Yep, the code is for if var in only. Regardless, the built in if var in/contains still beats the workarounds both in performance and readability...
For if var in, using InStr("abc,def,ghi", "bc,d"), returns true, so this is not reliable... I'd rather go with if ("def" ~= "i)^(abc|def|ghi)$")

Return to “AutoHotkey v2 Development”

Who is online

Users browsing this forum: JoeWinograd and 19 guests