How do I create nested dictionaries with upsert in KDB?

Viewed 65

Why does the d[c] assignment not work here?

d: `a`b!(1;2)
d
a| 1
b| 2
d[`c]: d
'type
  [0]  d[`c]: d

(PS it doesn't work with any dictionary, not just the recursive example shown here)

1 Answers

Your attempted assignment fails because you're trying to add to a "typed" dictionary (the type being long, in this case). You'll encounter the same error, trying to add a key-value pair with a symbol as the value, for example:

q)d[`c]:`s
'type
  [0]  d[`c]:`s

You can get around this by using a dictionary without a specified type for the values:

q)d:enlist[`]!enlist(::)
q)d[`a]:12.5
q)d[`b]:d
q)d
 | ::
a| 12.5
b| ``a!(::;12.5)
Related