In KRL, how to determine if a variable is an array or a hash?

In KRL, I would like to determine if a variable is an array or a hash so that I know if I need to use the decoding or encoding command on it. Is it possible?

I would like to do something like this:

 my_var = var.is_array => var.decode() | my_var
+3
source share
2 answers

Update The best way to do this is with the typeof () operator . This is new from the moment of the answer, but with an early interpretation of the variables, the old path specified in the answer will no longer work.

Another useful operator for examining your data is isnull ()

myHash.typeof() => "hash"
myArray.typeof() => "array"
...
+3
source

, , , - , , "" "".

" "

myHashIsHash = "#{myHash}".match(re/hash/gi);

myHashIsHash true/1

,

ruleset a60x547 {
  meta {
    name "detect-array-or-hash"
    description <<
      detect-array-or-hash
    >>
    author "Mike Grace"
    logging on
  }

  global {
    myHash = {
      "asking":"Mike Farmer",
      "question":"detect type"
    };
    myArray = [0,1,2,3];
  }

  rule detect_types {
    select when pageview ".*"
    pre {
      myHashIsArray = "#{myHash}".match(re/array/gi);
      myHashIsHash = "#{myHash}".match(re/hash/gi);
      myArrayIsArray = "#{myArray}".match(re/array/gi);
      myArrayIsHash = "#{myArray}".match(re/hash/gi);

      hashAsString = "#{myHash}";
      arrayAsString = "#{myArray}";
    }
    {
      notify("hash as string",hashAsString) with sticky = true;
      notify("array as string",arrayAsString) with sticky = true;

      notify("hash is array",myHashIsArray) with sticky = true;
      notify("hash is hash",myHashIsHash) with sticky = true;
      notify("array is array",myArrayIsArray) with sticky = true;
      notify("array is hash",myArrayIsHash) with sticky = true;
    }
  }
}

!

alt text

+2

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


All Articles