我有一堆散列数据,我正在从中挑选。有时会有数据可供选择,有时则不会。什么是最好的方法来知道何时选择运算符(operator)找到了某些东西,何时没有找到以便我可以在我的代码中对此使用react?
最佳答案
pick operator 将采用第二个可选参数,使其始终以数组形式返回结果。这意味着如果选择了某些东西,数组的长度将大于 0,否则它将为 0。然后您可以使用它来做您想做的事情。
来自 http://kynetxappaday.wordpress.com/2011/01/04/day-30-detecting-empty-pick/ 的示例代码/应用程序
ruleset a60x526 {
meta {
name "hash-pick-detect"
description <<
hash-pick-detect
>>
author "Mike Grace"
logging on
}
global {
dataHash = {
"one": {
"name": "Mike"
}, // number
"two": {
"random": 8
}, // number
"three": {
"name": "Alex"
} // number
}; // dataHash
} // global
rule detect_the_pick {
select when pageview ".*"
foreach dataHash setting (key, value)
pre {
userName = value.pick("$.name", true);
length = userName.length();
}
if (length > 0) then {
notify("Key: #{key}","Name: #{userName}<br/>Length: #{length}") with sticky = true;
}
notfired {
raise explicit event empty_pick
with pickedKey = key;
}
}
rule empty_pick_found {
select when explicit empty_pick
pre {
pickedKey = event:param("pickedKey");
results =<<
Key: #{pickedKey}<br/>
doesn't have a name associated with it to pick from
>>; //' fixing syntax highlighting
}
{
notify("An empty pick was detected",results) with sticky = true;
}
}
}
关于hash - 从 KRL 的哈希中检测空 pick(),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4589020/