从 KRL 中的哈希中检测空 pick()

发布于 2024-10-09 21:01:47 字数 94 浏览 5 评论 0原文

我在哈希中有一堆数据,我正在从中挑选。有时会有数据可供选择,有时则没有。知道拾取操作员何时发现某些内容以及何时未找到某些内容的最佳方法是什么,以便我可以在代码中对此做出反应?

I have a bunch of data in a hash and I am picking from it. Sometimes there will be data there to pick and sometimes there won't. What is the best way to know when there was something found by the pick operator and when there wasn't so I can react to that in my code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

落叶缤纷 2024-10-16 21:01:47

pick 运算符 将采用第二个可选参数,使其始终返回以下结果:一个数组。这意味着,如果选择了某些内容,则数组的长度将大于 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;
    }
  }
}

The pick operator will take a second optional parameter that will make it so that it always returns the results in an array. This means that if something is picked, the length of the array will be greater than 0, otherwise it will be 0. You can then use that to do what you are wanting to do.

Example code/app 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;
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文