问题
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