Skip to content

get, set

Read or set value of a variable or a kdb+ data file


Read or memory-map a kdb+ data file

Syntax: get x, get[x]

Reads or memory maps kdb+ data file x. A type error is signalled if the file is not a kdb+ data file.

Used to map columns of databases in and out of memory when querying splayed databases, and can be used to read q log files etc.

q)\l trade.q
q)`:NewTrade set trade                  / save trade data to file
q)t:get`:NewTrade                       / t is a copy of the table
q)`:SNewTrade/ set .Q.en[`:.;trade]     / save splayed table
q)s:get`:SNewTrade/                     / s has columns mapped on demand

get and value

get has several other uses. The function value is a synonym for get. By convention, it is used for other purposes. But the two are completely interchangeable.

q)value "2+3"
q)get "2+3"


Assign a value to a variable or file

Syntax: x set y, set[x;y]

Assigns the value of y to variable name or filename x

q)`a set 1 2 3            / set name a
1 2 3

q)a set 1 2 3             / set name t (indirect assignment)
1 2 3

q)a set 1 2 3             / fails, as name must be a symbol

If x is a filename, the values are written to file:

q)`:work.dat set 1 2 3    / write values to file
q)get `:work.dat
1 2 3

Write a table to a single file:

q)\l sp.q
q)`:mytable.dat set sp
q)get `:mytable.dat
s  p  qty
s1 p1 300
s1 p2 200
s1 p3 400

To save a table splayed across a directory, x must be a path (i.e. ends with a /), and the table must be fully enumerated, with no primary keys:

q)`:mydata/ set sp
q)\ls mydata
q)get `:mydata
s  p  qty
s1 p1 300
s1 p2 200
s1 p3 400

Avoid Kx namespaces

Avoid setting variables in the Kx namespaces, as undesired and confusing behaviour can result.

These are .h, .j, .Q, .q, .z, and any other namespaces with single-character names.

Basics: File system