Autohotkey:查找列表(对象)中的匹配项数
Autohotkey: Find number of matches in a list (object)
我需要在列表中查找匹配项的数量。现在我可以通过以下笨拙的方式达到目的:
aBlocks := ["A", "B", "C", "B"]
For key, value in aBlocks
{
a = 0
if (value == "B")
{
a += 1
}
}
msgbox, %a%
我想知道是否有现成的函数,例如object.count("B")
[这个不起作用,因为它只是return对象中所有值的计数]。
不,不存在这样的方法(或函数或命令)。
我觉得你做的很好。
您可以自己创建方法:
MyArray := [1, 1, 2, 3, 4, 1]
MyArray2 := ["hello", "hello", "cool", "array"]
MyObj := {color: "red"
, mileage: 86959
, model: "ford"
, price: "0"
, notes: "doesn't start"}
MsgBox, % MyArray.CountMatches(1)
MsgBox, % MyArray2.CountMatches("hello")
MsgBox, % MyObj.CountMatches("NoSuchValue")
Array(params*)
{
return Object(params*)
}
Object(params*)
{
return (params, params.base := CustomMethods)
}
class CustomMethods
{
CountMatches(needle)
{
for each, value in this
if(value == needle)
i++
return i ? i : 0
}
}
不过可能有点高级。自定义函数而不是方法肯定会更简单。虽然,方法看起来不错。
如果您需要,我可以稍后 explain/comment 该代码,现在没时间。刚写的很快。
我需要在列表中查找匹配项的数量。现在我可以通过以下笨拙的方式达到目的:
aBlocks := ["A", "B", "C", "B"]
For key, value in aBlocks
{
a = 0
if (value == "B")
{
a += 1
}
}
msgbox, %a%
我想知道是否有现成的函数,例如object.count("B")
[这个不起作用,因为它只是return对象中所有值的计数]。
不,不存在这样的方法(或函数或命令)。
我觉得你做的很好。
您可以自己创建方法:
MyArray := [1, 1, 2, 3, 4, 1]
MyArray2 := ["hello", "hello", "cool", "array"]
MyObj := {color: "red"
, mileage: 86959
, model: "ford"
, price: "0"
, notes: "doesn't start"}
MsgBox, % MyArray.CountMatches(1)
MsgBox, % MyArray2.CountMatches("hello")
MsgBox, % MyObj.CountMatches("NoSuchValue")
Array(params*)
{
return Object(params*)
}
Object(params*)
{
return (params, params.base := CustomMethods)
}
class CustomMethods
{
CountMatches(needle)
{
for each, value in this
if(value == needle)
i++
return i ? i : 0
}
}
不过可能有点高级。自定义函数而不是方法肯定会更简单。虽然,方法看起来不错。
如果您需要,我可以稍后 explain/comment 该代码,现在没时间。刚写的很快。