'Should I be able to assign in nested dictionaries in KDB?

According to the docs, the assignment of `lt below should have upsert mechanics:

s:()!()
s[`MSFT]:(`state`sym)!(`init`MSFT)

    | state sym
----| ----------
MSFT| init  MSFT
s[`MSFT][`lt]: 3

'assign
  [0]  s[`MSFT][`lt]: 3
                    ^

But instead I get an error.

Wham I doing wrong?

kdb


Solution 1:[1]

A keyed table is a map from a table to a table, so what you're indexing in to s with needs to itself be a table. So

(enlist `) ! enlist `MSFT

Second, if you are starting with an empty keyed table, you need to enlist the key and value.

q)s: () ! ()
q)s[enlist (enlist `) ! enlist `MSFT]: enlist (`state`sym) ! `init`MSFT
q)s
    | state sym 
----| ----------
MSFT| init  MSFT

When your table is no longer empty, you don't need to enlist the key and value.

q)s[(enlist `) ! enlist `GOOG]: (`state`sym) ! `init`GOOG
q)s
    | state sym 
----| ----------
MSFT| init  MSFT
GOOG| init  GOOG

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Matthew Greenlees