1
votes

The AutoHotkey documentation lists two methods of comparing a variable to items in a list.

if Var in MatchList
if Var contains MatchList

What's the difference between if var in and if var contains?

1
Per the documentation you pointed to: The name of the variable whose contents will be checked. For the "in" operator, an exact match with one of the list items is required. For the "contains" operator, a match occurs more easily: whenever Var *contains one of the list items as a substring.* So Var in MatchList is true only if Var is an exact match to a member of MatchList. For Var contains MatchList to be true, an element of MatchList is a substring of Var. Note in their examples, if var contains 1,3 compares 1 and 3 as strings. So if var is 23, it is true.lurker
suppose v=abcd. v in bc,xy fails, while v contains bc,xy passesJim U

1 Answers

4
votes

Hopefully some examples will help demonstrate the difference.

Example of if var in MatchList:

Match:

F3::
example := "pizza" ; Contains a matching string.
if example in This,is,a,tasty,pizza
    MsgBox, %example% found in MatchList

Negative match:

F3::
example := "pizz" ; Contains no matching string.
if example not in This,is,a,tasty,pizza
    MsgBox, %example% not found in MatchList

Example of if var contains MatchList:

Match:

F3::
example := "ice" ; Contains a matching "i" substring.
if example contains p,i,z,z,a
    MsgBox, %example% contains MatchList item(s)

Negative match:

F3::
example := "doggy" ; Contains no matching substring.
if example not contains p,i,z,z,a
    MsgBox, %example% does not contain MatchList item(s)