3

在 KRL 中,我想检测变量是数组还是散列,以便知道是否需要对其使用解码或编码运算符。那可能吗?

我想做这样的事情:

 my_var = var.is_array => var.decode() | my_var
4

2 回答 2

3

更新 最好的方法是使用typeof()运算符。这是自答案以来的新方法,但随着变量的早期解释,答案中列出的旧方法将不再适用。

另一个检查数据的有用运算符是isnull()

myHash.typeof() => "hash"
myArray.typeof() => "array"
...
于 2011-06-23T15:49:24.547 回答
2

我想出如何检测数据结构类型的唯一方法是强制转换为字符串,然后检查生成的指针字符串是否包含单词“array”或“hash”。

'一个班轮'

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

myHashIsHash 为真/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;
    }
  }
}

示例应用程序在行动!

替代文字

于 2011-01-18T18:10:50.767 回答