Getting started with Kdb
Contents
Entering Commands
Now that we have installed kdb+, when we start the q process we are presented with a q) prompt. This q prompt is an interactive read-eval-print loop. Commands can be entered, immediately they are evaluated and a result is returned, this instant feedback will help us learn kdb+ quickly. Look what happens when we enter some simple math commands:
Defining and using Variables
To store a value to a variable we enter the name, followed by a colon, followed by the value we want to save. Alternatively on the right hand side we could enter an expression, that would be evaluated and its result stored. To then retrieve these values we enter the name by itself, or we can use the variable within an expression. For Example:
For reason covered later in the course we recommend using camelCased variable names and avoiding underscores.
Comments
Comments can be placed at the end of a line after a space and single forward-slash " /
" like so:
Right-to-Left Evaluation
If we start to enter some more complicated mathematical expressions the value returned may not be what you expected.
Normally mathematical rules of precedence (BODMAS) mean that the multiplication should happen first,
we would expect the result 1010 = (100*10)+10
. However kdb+ evaluates expressions
right-to-left. There are no precedence rules. The reason commonly given for this behaviour
is that it is a much simpler to understand system, many q-gods would agree, beginners however may not.
Useful Commands
The most useful commands when starting to explore q environment will be the system
or slash
commands.
These are commands which can entered in two formats, as a string to the system function or as a slash followed by a single letter.
Some of the more common built-in commands are shown:
The kdb+ built-in commands are mostly a single letter. If a system
command that is not built-in is entered, that command will be passed to the underlying operating system.
i.e. A dos command in windows, or a Linux terminal. Below demonstrates the system command
being used to call the dos dir
command on windows:
Loading a Script
To load a q script we can use the system l
command with the name or path to our script
hello.q q script contents
Notice that we can use semi-colons ; to separate statements and use the show
command to print to the console.
Running the script from the console
Exiting kdb+
Finally to exit kdb+ we can either use the exit
command or two slashes "\\".
The exit command is more powerful and allows setting a return value.