首页 文章

存在q / kdb中的函数

提问于
浏览
5

我需要在q / kdb中编写一个函数,它接受一个变量v,如果定义了v,则返回1b,如果不是,则返回0b:

$ a:2
$ doesExist`a
1b
$ doesExist`b
0b

任何想法都赞赏 .

3 回答

  • 9
    key`.
    

    将为您提供当前命名空间中的所有变量 .

    同样

    key`.foo
    

    将为您提供 .foo 命名空间中的所有变量 .

    通过扩展:

    `a in key`.
    

    会给你你想要的布尔值

  • 4

    根据MdSalih的回答和小册子的评论,或许我们可以测试相反的情况 . 由于如果未定义变量,键输出一个空列表,我们应该测试它,这可以解决键控表问题 .

    q)AnswerToLifeUniverseAndEverything:42
    q)doesExist:{not () ~ key x}
    q)doesExist[`AnswerToLifeUniverseAndEverything]
    1b
    q)doesExist[`UltimateQuestionToLifeUniverseAndEverything]
    0b
    
  • 5
    q)doesExist:{x~key x}
    q)a:2
    q)doesExist`a
        1b
    q)doesExist`b
        0b
    

相关问题