如何检查字符串是否包含在 AutoHotKey 中的数组中

How to check if string is contained in an array in AutoHotKey

我有以下代码:

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

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

它 returns 0 即使字符串显然存在于数组中。

如何测试数组中是否存在字符串值?

PS:我也尝试了 if var in,结果相同。

你不能,只使用一个命令。自 1.1.22.3 起,AHK_L 中未实现此类功能。

您必须定义自己的函数

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
}

或者使用一些奇特的解决方法:

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

这将创建一个关联数组并将您的值作为键(此处的值设置为 0),因此使用 .HasKey() 很有意义。