Page 1 of 1

In and Contains plans...

Posted: 01 Jul 2014, 11:28
by Coco
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'}

Re: In and Contains plans...

Posted: 01 Jul 2014, 20:51
by lexikos
It's up in the air.

Re: In and Contains plans...

Posted: 01 Jul 2014, 21:02
by joedf
Interesting idea...

Re: In and Contains plans...

Posted: 01 Jul 2014, 21:41
by toralf
Is there actually any benefit over Regexmatch, instr() or ~=?
Besides that it is easier human readable?

Re: In and Contains plans...

Posted: 02 Jul 2014, 01:55
by Coco
@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.

Re: In and Contains plans...

Posted: 02 Jul 2014, 02:29
by toralf
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

Re: In and Contains plans...

Posted: 02 Jul 2014, 03:30
by Coco
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).

Re: In and Contains plans...

Posted: 02 Jul 2014, 16:25
by toralf
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%

Re: In and Contains plans...

Posted: 03 Jul 2014, 03:53
by Coco
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)$")