Detect empty selection () from hash in KRL

I have a bunch of data in a hash, and I choose from it. Sometimes there will be data, and sometimes they will not. What is the best way to find out when something is found by a select statement, and when it wasn't, can I react to it in my code?

+4
source share
1 answer

the select statement will take a second optional parameter, which will make it so that it always returns the results to an array. This means that if something is selected, the length of the array will be greater than 0, otherwise it will be 0. Then you can use this to do what you want to do.

Sample code / application taken from 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; } } } 
+4
source

Source: https://habr.com/ru/post/1334133/


All Articles