I am still a KDB noob. This page will be kept updating whenever I have new tricks.
Display string to console**
0N!"Hello World" Cast string to symbol
ticker:"MSFT" ticker_symbol:`$ticker List
Define a list
mylist:(1;2;3) Get n-th element
(1;2;3)[1] {x[1]}(1;2;3) Get first n elements
3#(1;2;3;4;5;6;7;8;9;10) Get last n elements
-3#(1;2;3;4;5;6;7;8;9;10) Filter list
even_list:{x where not x mod 2}(1;2;3;4;5;6) Run function on each element
{0N!x} each ("Hello";"World") ({0N!x}')("Hello";"World") Convert string list to long list
({"J"$x}')("123";"456";"789") Command line parameters
This is the command we use.
rlwrap q myscript.q -foo "bar" "123" .z.x
arguments:.z.x / arguments is a list (0h) of ("-foo";"bar";"123") .z.X
arguments:.z.X / arguments is a list (0h) of ("q";"myscript.q";"-foo";"bar";"123") .Q.opt .z.X
arguments:.Q.opt .z.X / arguments is a dict (99h) of (enlist `foo)!enlist ("bar";"123") String
Get substring
6 8 sublist "Hello world" 6 _ "Hello world" Join strings
"Hello"," ","World" "" sv ("Hello";enlist " ";"World") Find occurrences of a substring in a string
ss["Hello world";"wo"] File IO
Open a file where file name is in variable
filePath:"/tmp/test.txt" file:hopen hsym[`$filePath] Delete a file
hdel `:/home/franz/test.txt Check if a file exists
not () ~ key hsym `:/home/franz/test.txt Delete a file only if it exists
if[not () ~ key hsym `:/home/franz/test.txt; hdel `:/home/franz/test.txt] Write a single string to file (Not append)
`:test.txt 0: enlist "Hello world" Table
Define a table
table1:([] c1:1 2; c2:`abc`def; c3:123 456) Define a single row table
table1:([] enlist c1:1; c2:`abc; c3:123) Export a table to csv
File name is same as the table name.
save `$"/home/franz/table1.csv" Export a table to csv with a different file name
`:/home/franz/test.txt 0: csv 0: table1 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" Conditional branching
try-catch
h:@[hopen;`::9999;{show "Unable to connect"; exit 1}] Early return
Use : without anything on the left
:"Hello World"
Top comments (0)