Entries in \ (2)

How can I test for the existence of a file or directory in q?

Short answer: key file_handle or key dir_handle

File handle example:

q)\touch /tmp/test.txt
q)key hsym ` $ "/tmp/test.txt"
`:/tmp/test.txt
q)

The function key returns an empty list when the file does not exist:

q)hdel `:/tmp/test.txt
`:/tmp/test.txt
q)() ~ key `:/tmp/test.txt
1b
q)

key is one of the most heavily overloaded functions in q. It accepts an argument of type dictionary, keyed table, table column, list, and integer. It's easy to confuse it with keys, which turns out to be an entirely different function.

See also: ~ (match), \ (system), hdel, hsym

How do I debug code in kdb?

You can enable error trapping via the -e command line option at kdb start-up:

$ q -e 1

or from the q console

q)\e 1

Upon an error, kdb halts and outputs the body of the function (.z.s (self)) in which the error occurred as well as an error message. You are free to inspect the values of any global or local variables to try to diagnose the source of the problem. At this point you have the following options:

  • type '(single quote) to pop the stack.
  • type :(colon) to resume execution
  • type \(slash) to exit debug mode

There is no ability step into a function call or move up and down the stack.