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

瘦欲@ 提交于 2019-12-10 14:23:08

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/33591667/how-to-check-if-string-is-contained-in-an-array-in-autohotkey

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!