cancel
Showing results for 
Search instead for 
Did you mean: 

KDB table with an array as element

renbright
New Contributor

I want to create a KDB table with an array:

- for a regular one, I would have t:([]time:`time$();price:`float$()); for each time, there is only price

- I want to have a table with a price array for each time. should I create it using t:([]time:`time$();price:())  or t:([]time:`time$();price:(()))

2 REPLIES 2

darrenwsun
New Contributor III

Both works, as (()) is interpreted the same as (). For this reason the first form is preferred. Note that the data type of price is determined by the data type of cell in the first row.

 

q)t:([] time:`time$(); price:())
q)meta t
c    | t f a
-----| -----
time | t
price|
q)`t upsert (.z.t; 10 11f)
`t
q)t
time         price
------------------
21:42:07.028 10 11
q)meta t
c    | t f a
-----| -----
time | t
price| F
q)`t upsert (.z.t; 10 11)
`t
q)t
time         price
------------------
21:42:07.028 10 11
21:44:30.724 10 11
q)meta t
c    | t f a
-----| -----
time | t
price| F

 

gdickson
Moderator Moderator
Moderator

To define any column in a table as a list or an array, you can simply specify the column type with an empty set of parentheses, (), on definition. Following this definition, you can then upsert the price array along with the time value, and the column value for that table will be assigned to a float list type (upper-case F in meta command indicates the column is a list of floats, as opposed to a float atom which would be identified with a lower case f):

q)t:([]time:`time$();price:())
q)`t upsert (.z.t;60.57 60.61)
`t
q)t
time         price
------------------------
21:47:06.298 60.57 60.61
q)meta t
c    | t f a
-----| -----
time | t
price| F