2022.01.21 06:41 AM
Hi, what is the function of the symbol ` ? Sorry I don't know how to call it
Sometimes I find it together with $ . For example:
a: "1"
show b: ` $a
Thanks
2022.01.21 07:21 AM
Hi Ypangalela,
Backtick represents a symbol. In your example the function of $ is to cast (or transform) a string to a symbol - see more examples here https://code.kx.com/q4m3/7_Transforming_Data/#732-creating-symbols-from-strings
and also here https://kx.com/learning/modules/casting/#casting-methods
Hope this is helpful/
Thanks,
Michaela
2022.01.24 01:52 AM - edited 2022.01.24 01:57 AM
example #1
example #2
So the reason for this behavior has more to do with function scoping than backtick. You can learn more about function scoping (defining locally verses globally) here, specifically around 4.30mins you can see in action the two ways of defining variables globally - using set and double colon.
Hope this helps!
2022.01.21 07:21 AM
Hi Ypangalela,
Backtick represents a symbol. In your example the function of $ is to cast (or transform) a string to a symbol - see more examples here https://code.kx.com/q4m3/7_Transforming_Data/#732-creating-symbols-from-strings
and also here https://kx.com/learning/modules/casting/#casting-methods
Hope this is helpful/
Thanks,
Michaela
2022.01.21 10:52 AM - edited 2022.01.21 10:59 AM
Also, this just brings up good questions about symbols versus strings & when and why we use them.
Many languages including q intern strings so that only one copy of any distinct value is stored. Any other references to the same string are stored as just that, references.
This helps with memory management, and improves performance on equality comparison i.e.
// assuming col1 is symbol column
q)select from tab where col1 in `a`b
Storing the same string e.g. "New York Stock Exchange" repeatedly in memory would require a greater amount of memory than its symbol counterpart.
Because q typically stores the string pool entirely in memory (that's what your sym file is in a typical kdb+ database) consideration should be given as to what columns are symbol and which are string.
ℹ️ If the string list contains many short repeated values this may be a good candidate for casting to a symbol
2022.01.23 06:30 AM
Hi Michaela, thanks for the answer
Now I'm studying about function and encountering the same problem about backtick.
Let me give 2 examples.
Example #1:
b:6
f:{b:42; `b set x; b}
f[98]
b
Example #2:
b:6
f:{b:42; b set x; b}
f[98]
b
In example #1, the value of b becomes 98, but in example #2, it stays 6
In this case, what's the function of backtick?
Thanks
2022.01.24 01:52 AM - edited 2022.01.24 01:57 AM
example #1
example #2
So the reason for this behavior has more to do with function scoping than backtick. You can learn more about function scoping (defining locally verses globally) here, specifically around 4.30mins you can see in action the two ways of defining variables globally - using set and double colon.
Hope this helps!
EMEA
Tel: +44 (0)28 3025 2242
AMERICAS
Tel: +1 (212) 447 6700
APAC
Tel: +61 (0)2 9236 5700
KX. All Rights Reserved.
KX and kdb+ are registered trademarks of KX Systems, Inc., a subsidiary of FD Technologies plc.