Skip to content

keys, xkey

Get or set key column/s of a table


Key column/s of a table

Syntax: keys x, keys[x]

Where x is a table (by value or reference), returns as a symbol vector the primary key column/s of x – empty if none.

q)\l trade.q        / no keys
q)keys trade
q)`sym xkey`trade   / define a key


Set specified columns as primary keys of a table

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

Where symbol atom or vector x lists columns in table y, which is passed by

  • value, returns
  • reference, updates

y with x set as the primary keys.

q)\l trade.q
q)keys trade
`symbol$()            / no primary key
q)`sym xkey trade     / return table with primary key sym
sym| time         price size
---| -----------------------
a  | 09:30:00.000 10.75 100
q)keys trade         / trade has not changed
q)`sym xkey `trade   / pass trade by reference updates the table in place
q)keys trade         / sym is now primary key of trade

.Q.ff (append columns)
Basics: Dictionaries & tables, Metadata