如何检查字符串是否包含在AutoHotKey中的数组中 [英] How to check if string is contained in an array in AutoHotKey
本文介绍了如何检查字符串是否包含在AutoHotKey中的数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码:
ignored := [ "Rainmeter.exe", "Nimi Places.exe", "mumble.exe" ]
a := ignored.HasKey("mumble.exe")
MsgBox,,, %a%
即使字符串明显存在于数组中,它也会返回0
.
It returns 0
even though the string is clearly present in the array.
如何测试数组中是否存在字符串值?
PS:我也尝试过if var in
,它给出相同的结果.
PS: I also tried if var in
which gives same results.
推荐答案
您不能仅使用一个命令.从1.1.22.3版开始,AHK_L中未实现此类功能.
You can't, using just one command. Such functionality is not implemented in AHK_L as of 1.1.22.3.
您必须定义自己的函数
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")
这将创建一个关联数组,并将您的值用作键(此处的值设置为0),因此.HasKey()
很有用.
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.
这篇关于如何检查字符串是否包含在AutoHotKey中的数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文