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?
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