Day 34 – Dynamically Picking From Hash

Sometimes I don’t want to statically pick from a dataset or a hash. Sometimes I want to dynamically pick from a hash or dataset based on a condition and I can do that by using a beesting in a pick statement.

ruleset a60x545 {
  meta {
    name "beesting-in-pick"
    description <<
      beesting-in-pick
    >>
    author "Mike Grace"
    logging on
  }

  rule test_pick {
    select when pageview ".*"
    pre {
      r = math:random(2);
      pleasePick = (r == 0) => "userName" | "cool";
      myHash = {
        "userName":"mike",
        "cool":"yes"
      };
      picked = myHash.pick("$.#{pleasePick}");
    }
    {
      notify("picked","Picked with: #{pleasePick}<br/>Picked value: #{picked}") with sticky = true;
    }
  }
}
  • 14 get random number to simulate picking based on condition
  • 15 set pick based on random based condition
  • 20 get value from pick statement

App run several times in a row on example.com with a bookmarklet

Get the bookmarklet to try it out yourself!

Gratuitous day 34 Grace face

About these ads
This entry was posted in Kynetx. Bookmark the permalink.

One Response to Day 34 – Dynamically Picking From Hash

  1. Phil Windley says:

    The more general idea here is that the pick specification is just a string and there are lots of ways to construct a string. Beestings are among the most convenient, but it’s also possible, as an example, to get the pick string from one hash and use it to pick on another.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s