如何在Kdb+中以正确的形式引用一个变量?

huangapple go评论80阅读模式
英文:

How to refer a variate with correct form in Kdb+?

问题

在第3行,xp 都指代一个表变量,但在 $(枚举)的调用中,需要使用 `x(作为符号)来引用相关的表,而直接使用 p(不加反引号)来引用其他表。

什么情况下应该在名称前加上反引号,什么情况下不用?

英文:

I am using cli of Q. I found there are always 2 ways to refer a variable amid the context, as the belowing codes:

q)p:`AAPL`AAPL`GE`IBM`GE`IBM`GE`GOOG`GE`IBM`AAPL
q)x:`u#distinct p / apply the unique attribute
q)e:`x$p

At the 3rd line, x and p both refer to a table variate, but in the call of $(enumeration), it is required to use `x (as a symbol) to refer the related table, while use p directly (without back quote) to refer the other.

When shoudl I use the name with a back quote prefixed, and when not?

答案1

得分: 2

确切的答案将取决于操作,因为不同的运算符接受不同类型。特别是枚举需要其左参数中的符号和右参数中的符号列表。符号包含变量名称是操作符的前提条件的一部分。实际上,你可以在左边使用一个变量(x$p),但这仅在变量x包含一个符号时才有效,然后由该符号命名的变量将成为枚举域。

其他使用符号重要的地方包括表格修改操作(例如updatedeletexkey)。如果你提供变量名称,那将被视为表达式,并且你将获得已修改的表格,而不会修改原始变量。另一方面,如果你提供一个符号,它将被视为名称,并且具有该名称的变量将被更新。

英文:

The exact answer will depend on the operation, since different operators accept different types. Enumeration in particular requires a symbol in its left argument and a symbol list in the right one. The fact that the symbol contains the name of a variable is part of the preconditions of the operator. In fact you could use a variable on the left side (x$p) but that would only work if the variable x contained a symbol, and then the variable named by that symbol would be the enumeration domain.

Other places where the usage of symbols is significant are table modifying operations (e.g. update, delete and xkey). If you provide the variable name, that counts as an expression and you get the modified table without the original variable being modified. On the other hand if you provide a symbol, it counts as a name and the variable with that name will be updated.

答案2

得分: 2

根据您执行的操作而定。

https://code.kx.com/q 网站可以指导您何时使用`

包括有关枚举的文档。

通常情况下,在变量名称前加上`可以被视为在语言想要引用全局对象而不是使用其值时使用。

一个很好的示例,` 可以用于不同的含义来编写您的代码,可以查看 https://code.kx.com/q/ref/update/

q)t:([] a:1 2 3)
q)update a+1 from t //执行更新并返回更新后的表
a
-
2
3
4
q)t2:update a+1 from t //执行更新并将结果存储在t2中
q)t2 
a
-
2
3
4
q)t //全局表t保持独立且不变
a
-
1
2
3
q)update a+1 from `t //对全局t变量执行就地更新
`t
q)t //t已经被更新
a
-
2
3
4
英文:

It depends based on the operations you are performing.

The https://code.kx.com/q website can guide you on when to use `

Including documentation on enumeration.

` prefixing the variable name can generally be seen to be used when the language wants to reference a global object rather than using it's value.

A good example where the ` can be used or not to give different meaning to your code is https://code.kx.com/q/ref/update/

q)t:([] a:1 2 3)
q)update a+1 from t //Perform an update and return the updated table
a
-
2
3
4
q)t2:update a+1 from t //Perform an update and store the result in t2
q)t2 
a
-
2
3
4
q)t //The global t table remains independent and unchanged
a
-
1
2
3
q)update a+1 from `t //Perform an in-place update to the global t variable
`t
q)t //t has been updated
a
-
2
3
4

答案3

得分: 1

与枚举相关的另一个示例:

q)s:ab`c

//使用“?”并按值引用对象会导致“find”(索引查找)
q)s?cdba
2 3 1 0

//使用“?”并按名称引用对象会导致枚举扩展(如果需要,扩展枚举列表)。
q)s?cdba s$cdba

//向枚举域添加了“d”
q)s
abcd

最终,这些重载的操作是允许kdb在有限的字符列表上执行许多操作的关键。解释器识别不同的重载。
英文:

One additional example which is relevant to enumeration:

q)s:`a`b`c

/using "?" and referencing the object by value results in a "find" (index lookup)
q)s?`c`d`b`a
2 3 1 0

/using "?" and referencing the object by name results in an enum-extend (enumeration, extending the enumeration list if required). 
q)`s?`c`d`b`a
`s$`c`d`b`a

/"d" was added to the enumeration domain
q)s
`a`b`c`d

Ultimately these overloaded operations are what allows kdb to perform many operations on a limited character list. The interpreter recognises the different overloads.

huangapple
  • 本文由 发表于 2023年6月19日 21:45:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76507242.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定