KDB+ how to update nested dictionaries?

Viewed 241

I have a dictionary of dictionaries called books.

q)type books
99h
q)key books
`ETH-USD-BIDS`ETH-USD-ASKS`BTC-USD-BIDS`BTC-USD-ASKS
q)type books[`$"ETH-USD-BIDS"]
99h
q)key books[`$"ETH-USD-BIDS"]
3518.12 3517.97 3517.96 3517.86 3517.85 3517.84 3517.63 3517.58 3517.51 3517.43 3517.26..

I can successfully index into each book to find volumes at each price level

q)books[`$"ETH-USD-BIDS"][3518.12]
.035

But when I try to update a specific price level with a new volume, I get an 'assign error.

q)books[`$"ETH-USD-BIDS"][3518.12]:4f
'assign
  [0]  books[`$"ETH-USD-BIDS"][3518.12]:4f

Can anyone help me understand how to assign new volumes to my price levels? I'm somewhat perplexed. Thanks!!

1 Answers

Use ; to index/amend at depth rather than several [].

q)dict:(`a`b`c)!(`d`e`f!til 3;`g`h`i!2*til 3;`j`k`l!1+til 3)
q)dict
a| `d`e`f!0 1 2
b| `g`h`i!0 2 4
c| `j`k`l!1 2 3

q)dict[`a;`d]:20
q)dict
a| `d`e`f!20 1 2
b| `g`h`i!0 2 4
c| `j`k`l!1 2 3

/ q operates right to left so this is evaluating first giving the error
q)[3518.12]:4f
'assign
  [0]  [3518.12]:4f
Related