Search code examples
stringcomparisonautohotkey

Comparing a variable to multiple strings in Autohotkey


Often in my AutoHotkey scripts I need to compare a variable to several values values

if (myString == "val1" || myString == "val2" || myString == "val3" || myString == "val4")
    ; Do stuff

In most languages, there are ways to make this comparison a bit more concise.

Java

if (myString.matches("val1|val2|val3"))
    // Do stuff

Python

if myString in ["val1","val2","val3","val4"]
    # Do stuff

Does AutoHotkey have anything similar? Is there a better way to compare a variable against multiple strings?


Solution

  • Many different ways.

    • Autohotkey way (if Var in MatchList)

      if myString in val1,val2,val3,val4
      
    • Similar to your java regex based way

      if (RegExMatch(myString, "val1|val2|val3|val4"))
      
    • Similar to your python though not as nice way based on Associative Arrays

      if ({val1:1,val2:1,val3:1,val4:1}.hasKey(myString))