8

I have following code:

ignored := [ "Rainmeter.exe", "Nimi Places.exe", "mumble.exe" ]

a := ignored.HasKey("mumble.exe")
MsgBox,,, %a%

It returns 0 even though the string is clearly present in the array.

How do I test if a string value is present in an array?

PS: I also tried if var in which gives same results.

monnef
  • 3,903
  • 5
  • 30
  • 50

1 Answers1

7

You can't, using just one command. Such functionality is not implemented in AHK_L as of 1.1.22.3.

You'll have to either define your own function

hasValue(haystack, needle) {
    if(!isObject(haystack))
        return false
    if(haystack.Length()==0)
        return false
    for k,v in haystack
        if(v==needle)
            return true
    return false
}

or use some fancy workaround:

ignored := { "Rainmeter.exe":0, "Nimi Places.exe":0, "mumble.exe":0 }
msgbox, % ignored.HasKey("mumble.exe")

This would create an associative array and put your values as keys (the values are set to 0 here), so the .HasKey() makes sense to use.

phil294
  • 10,038
  • 8
  • 65
  • 98
  • 4
    Thanks for answer, `hasValue` function is a nice solution. I'm a bit surprised this functionality is not already part of AHK. – monnef Nov 08 '15 at 12:51