When are double quotes required to create a KDB/q symbol?

2.1k Views Asked by At

Normally, for simple character strings, a leading backtick does the trick.

Example: `abc

However, if the string has some special characters, such as space, this will not work.

  • Example: `$"abc def"
  • Example: `$"BAT-3Kn.BK"

What are the rules when $"" is required?

2

There are 2 best solutions below

0
On

Simple syntax for symbols can be used when the symbol consists of alphanumeric characters, dots (.), colons (:), and (non-leading) underscores (_). In addition, slashes (/) are allowed when there is a colon before it. Everything else requires the `$"" syntax.

0
On

The book 'Q for mortals', which is available online, has a section discussing datatypes. For symbols it states:

A symbol can include arbitrary text, including text that cannot be directly entered from the console – e.g., embedded blanks and special characters such as back-tick. You can manufacture a symbol from any text by casting the corresponding list of char to a symbol. (You will need to escape special characters into the string.) See §6.1.5 for more on casting.

q)`$"A symbol with blanks and `"
`A symbol with blanks and `

The essential takeaway here is that converting a string to a symbol is required when special characters are involved. In the examples you have given both space " " and hyphen "-" are characters that cannot be directly placed into a symbol type.