DEV Community

Franz Wong
Franz Wong

Posted on • Edited on

KDB cookbook

I am still a KDB noob. This page will be kept updating whenever I have new tricks.

Display string to console**

0N!"Hello World"
Enter fullscreen mode Exit fullscreen mode

Cast string to symbol

ticker:"MSFT"
ticker_symbol:`$ticker
Enter fullscreen mode Exit fullscreen mode

List

Define a list

mylist:(1;2;3)
Enter fullscreen mode Exit fullscreen mode

Get n-th element

(1;2;3)[1]
Enter fullscreen mode Exit fullscreen mode
{x[1]}(1;2;3)
Enter fullscreen mode Exit fullscreen mode

Get first n elements

3#(1;2;3;4;5;6;7;8;9;10)
Enter fullscreen mode Exit fullscreen mode

Get last n elements

-3#(1;2;3;4;5;6;7;8;9;10)
Enter fullscreen mode Exit fullscreen mode

Filter list

even_list:{x where not x mod 2}(1;2;3;4;5;6)
Enter fullscreen mode Exit fullscreen mode

Run function on each element

{0N!x} each ("Hello";"World")
Enter fullscreen mode Exit fullscreen mode
({0N!x}')("Hello";"World")
Enter fullscreen mode Exit fullscreen mode

Convert string list to long list

({"J"$x}')("123";"456";"789")
Enter fullscreen mode Exit fullscreen mode

Command line parameters

This is the command we use.

rlwrap q myscript.q -foo "bar" "123"
Enter fullscreen mode Exit fullscreen mode

.z.x

arguments:.z.x
/ arguments is a list (0h) of ("-foo";"bar";"123")
Enter fullscreen mode Exit fullscreen mode

.z.X

arguments:.z.X
/ arguments is a list (0h) of ("q";"myscript.q";"-foo";"bar";"123")
Enter fullscreen mode Exit fullscreen mode

.Q.opt .z.X

arguments:.Q.opt .z.X
/ arguments is a dict (99h) of (enlist `foo)!enlist ("bar";"123")
Enter fullscreen mode Exit fullscreen mode

String

Get substring

6 8 sublist "Hello world"
Enter fullscreen mode Exit fullscreen mode
6 _ "Hello world"
Enter fullscreen mode Exit fullscreen mode

Join strings

"Hello"," ","World"
Enter fullscreen mode Exit fullscreen mode
"" sv ("Hello";enlist " ";"World")
Enter fullscreen mode Exit fullscreen mode

Find occurrences of a substring in a string

ss["Hello world";"wo"]
Enter fullscreen mode Exit fullscreen mode

File IO

Open a file where file name is in variable

filePath:"/tmp/test.txt"
file:hopen hsym[`$filePath]
Enter fullscreen mode Exit fullscreen mode

Delete a file

hdel `:/home/franz/test.txt
Enter fullscreen mode Exit fullscreen mode

Check if a file exists

not () ~ key hsym `:/home/franz/test.txt
Enter fullscreen mode Exit fullscreen mode

Delete a file only if it exists

if[not () ~ key hsym `:/home/franz/test.txt; hdel `:/home/franz/test.txt]
Enter fullscreen mode Exit fullscreen mode

Write a single string to file (Not append)

`:test.txt 0: enlist "Hello world"
Enter fullscreen mode Exit fullscreen mode

Table

Define a table

table1:([] c1:1 2; c2:`abc`def; c3:123 456)
Enter fullscreen mode Exit fullscreen mode

Define a single row table

table1:([] enlist c1:1; c2:`abc; c3:123)
Enter fullscreen mode Exit fullscreen mode

Export a table to csv

File name is same as the table name.

save `$"/home/franz/table1.csv"
Enter fullscreen mode Exit fullscreen mode

Export a table to csv with a different file name

`:/home/franz/test.txt 0: csv 0: table1
Enter fullscreen mode Exit fullscreen mode

Load csv file to table

"jsj" is the data type of each column in the csv. For example, j is long and s is symbol.

table3:("jsj";enlist",")0:`$"/home/franz/table1.csv"
Enter fullscreen mode Exit fullscreen mode

Conditional branching

try-catch

Execution Control

h:@[hopen;`::9999;{show "Unable to connect"; exit 1}]
Enter fullscreen mode Exit fullscreen mode

Early return

Use : without anything on the left

:"Hello World"
Enter fullscreen mode Exit fullscreen mode

Top comments (0)