if var in/contains comma-separated list/array

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

Re: if var in/contains comma-separated list/array

25 Sep 2021, 22:49

iseahound wrote:
25 Sep 2021, 18:39
^(?i:Mon|Tue|Wed|Thu|Fri|Sat|Sun)$ is for the person writing the interpreter.
we're using "iAD)(exe|bat|com)"

https://github.com/mmikeww/AHK-v2-script-converter/blob/a6aa84fa40a764246f3639b422ba6c2f8223fa01/ConvertFuncs.ahk#L800

lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Re: if var in/contains comma-separated list/array

26 Sep 2021, 02:36

A) anchors to the start but D) doesn't do anything on its own. It only affects the behaviour of $, which you still need to append to the pattern.

You could just use \z to match at the end of the subject.

kczx3 wrote:
03 Aug 2021, 14:28
if RegExMatch(var, "^exe$|^bat$|^com$"
I considered that approach as well which would work as long as what you're trying to match is only a single line string.
It should work just as well with multi-line strings (since it lacks the "m" option), as long the subject doesn't have a trailing line ending (since it lacks the "D" option). I'd bet there are many scripts around that don't take into account the special case of a trailing line ending.
Last edited by lexikos on 11 Oct 2021, 02:38, edited 1 time in total.
Reason: Replaced \Z (which permits a newline at the end) with \z
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: if var in/contains comma-separated list/array

26 Sep 2021, 07:23

So in order to really see how the performance of the lookups scale I tried to create a Mp with 20 Million entries.
To my dismay it has yet to finish executing after roughly 13 hours.
Performance appears to be dropping even further.
Of course adding to a sorted list is a O(n²) operation - (unlike binary search trees with O(log(n)) or HashSets with O(1) addition time) - but even that doesn't explain how poor the performance is.
What I assume happens is that all 10000000 - 10999999 get sorted before the entry 11 having to shift all the entries in the list everytime one such entry is added.
This is a case where the O notation simply does not convey how much slower a binary search list is in comparison to a binary search tree or a hash map.

In comparison adding 20 million entries like that in java took 5 seconds out of which 4 seconds were spent compiling.
Recommends AHK Studio
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: if var in/contains comma-separated list/array

28 Sep 2021, 17:02

Finally its done
---------------------------
Performance.ahk
---------------------------
RegexMatch produced 1234567 hits in 25.559957399999998 seconds
the map produced 1234567 hits in 0.52017780000000002 seconds
---------------------------
OK
---------------------------
Its 0.52 seconds using iseahounds profiling code.
Also correction from last time - if I give adding to sorted lists a O(n²) I have to mean the operation of adding n entries as such BnaryTrees and HashSets would have had O(n*log(n)) and O(n) respectively.
Recommends AHK Studio
MrDoge
Posts: 160
Joined: 27 Apr 2020, 21:29

Re: if var in/contains comma-separated list/array

28 Sep 2021, 17:57

@lexikos

so I should add D ?

we only want it to match when it's exact match.
v2:

Code: Select all

Msgbox "Mon`n" ~= "i)^(Mon|Tue|Wed|Thu|Fri|Sat|Sun)$" ? "Matched" : "Not Matched" ;"Matched" shouldn't match since "Mon`n"!="Mon"
Msgbox "Mon`n" ~= "Di)^(Mon|Tue|Wed|Thu|Fri|Sat|Sun)$" ? "Matched" : "Not Matched" ;"Not Matched"
Msgbox "Mon`n" ~= "Di)^(Mon`n|Tue|Wed|Thu|Fri|Sat|Sun)$" ? "Matched" : "Not Matched" ;"Matched", perfect
v1:

Code: Select all

var:="hello`n"
if var in hello
{
  msgbox % "Matched"
} else {
  msgbox % "Not Matched" ;HERE
}

var:="hello`n"
if var in hello`n
{
  msgbox % "Matched" ;HERE
} else {
  msgbox % "Not Matched"
}
safetycar
Posts: 435
Joined: 12 Aug 2017, 04:27

Re: if var in/contains comma-separated list/array

29 Sep 2021, 14:15

Not lexikos here but...
The pcre.txt linked in the documentation says this about these anchors:

Code: Select all

     ^       start of subject
              also after internal newline in multiline mode
     \A      start of subject
     $       end of subject
              also before newline at end of subject
              also before internal newline in multiline mode
     \Z      end of subject
              also before newline at end of subject
     \z      end of subject
It seems from ahk docs that what D does is make $ as strict as \z.
So if you want to be as strict as possible add the D or actually use \z "i)\A(Mon|Tue|...)\z"

Here the result of some tests for a variable ending in `r`n, with a needle that doesn't:
var := "val`r`n"

MATCHED by "i)^(val)$"
MATCHED by "i)\A(val)\Z"

NOT matched by "iD)^(val)$"
NOT matched by "i)\A(val)\z"
The non-matching are suppose to be the desired ones, they'll match when the needle contains `r`n.

The code for the output above:

Code: Select all

Var := "val`r`n"
Txt := "var := ""val``r``n""`n`n"
Patt := "i)^(val)$"
Txt .= (var~=Patt ? "MATCHED" : "NOT matched") . " by """ Patt """`n"
Patt := "i)\A(val)\Z"
Txt .= (var~=Patt ? "MATCHED" : "NOT matched") . " by """ Patt """`n"
Patt := "iD)^(val)$"
Txt .= (var~=Patt ? "MATCHED" : "NOT matched") . " by """ Patt """`n"
Patt := "i)\A(val)\z"
Txt .= (var~=Patt ? "MATCHED" : "NOT matched") . " by """ Patt """`n"
MsgBox % Txt
MrDoge
Posts: 160
Joined: 27 Apr 2020, 21:29

Re: if var in/contains comma-separated list/array

29 Sep 2021, 14:36

@safetycar
thanks
"i)^(val)\z" looks less intrusive than
"Di)^(val)$"

I wouldn't use
"iD)^(val)$"
because it would mess up Ctrl+F Find : "id"

test: it works

Code: Select all

str:=""
str.=("Mon`r`n" ~= "i)^(Mon|Tue|Wed|Thu|Fri|Sat|Sun)\z" ? "Matched" : "Not Matched") "`n" ;"Not Matched"
str.=("Mon`r`n" ~= "i)^(Mon`n|Tue|Wed|Thu|Fri|Sat|Sun)\z" ? "Matched" : "Not Matched") "`n" ;"Not Matched"
str.=("Mon`r`n" ~= "i)^(Mon`r`n|Tue|Wed|Thu|Fri|Sat|Sun)\z" ? "Matched" : "Not Matched") "`n" ;"Matched"
Msgbox str
Edit: nervermind, ^(val)$ is way clearer
guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

Re: if var in/contains comma-separated list/array

07 Oct 2021, 11:33

lexikos wrote:
26 Sep 2021, 02:36
A) anchors to the start but D) doesn't do anything on its own. It only affects the behaviour of $, which you still need to append to the pattern.
i knew i wasnt smart enough to come up with AD) on my own

from the docs:
https://www.autohotkey.com/docs/commands/IfIn.htm wrote: The operators "in" and "contains" are not supported in expressions. Instead, use If statements such as if (Var ~= "iAD)Value1|Value2") for "in" or if (Var ~= "i)Value1|Value2") for "contains" to simulate the behavior of these operators.
might want to change that

iseahound
Posts: 1444
Joined: 13 Aug 2016, 21:04
Contact:

Re: if var in/contains comma-separated list/array

01 May 2022, 18:41

Although not mentioned in jeeswg's opening post, the most rational choice to me is:
if string contains "Sun","Mon","Tues","Wed","Thur","Fri","Sat"
where contains is really just the reverse of in:
if "Sun","Mon","Tues","Wed","Thur","Fri","Sat" in string
and "Sun","Mon","Tues" is a pseudo tuple type. It doesn't have to be an actual tuple type like in Python. But the reason why it should be comma deliminated, is because the output variables in for...in are also comma deliminated.

Finally, I think it is a mistake to compare across types. Having an array like if string contains ["Sun","Mon","Tues","Wed","Thur","Fri","Sat"] is wrong, because it's not clear what the purpose of the array notation offers. To unpack the array use: if string contains ["Sun","Mon","Tues","Wed","Thur","Fri","Sat"]*

A little note about python: They automatically unpack enumerables like max([1, 2, 3]). But in AHK unpacking must be specified: max([1, 2, 3]*)

Return to “AutoHotkey Development”

Who is online

Users browsing this forum: No registered users and 44 guests