user35631
user35631

Reputation: 37

how do i update or delete nested dictionary in kdb?

I would like to delete or update values in nested dictionary

Eg.

d:`date`tab`col!((2022.12.01;2022.12.03);`TRADE`SYM;`ID`CODE`PIN`NAME)

I would like to update `PIN to `Yen or maybe delete `PIN and `CODE from the dictionary.

Upvotes: 1

Views: 202

Answers (4)

pJfullen1804
pJfullen1804

Reputation: 248

You could also use amend in an implicit function to update nested values in a dictionary:

{.[d;(`col;x);:;`your`update]} where d[`col] in `ID`PIN

Output:

date| 2022.12.01 2022.12.03
tab | `TRADE`SYM
col | `your`CODE`update`NAME

Upvotes: 1

SJT
SJT

Reputation: 1097

Very minor mods to @Thomas Smyth-Treliant:

@[d;`col;] {x^(.[!]1#'`PIN`Yen) x}   / (1)
@[d;`col;] except[;`PIN`Yen]         / (2)

Upvotes: 1

Thomas Smyth
Thomas Smyth

Reputation: 5644

I think this may be slightly fiddly due to the nested nature but replacing values could be done with a dictionary and fills. This would replace all instances of PIN if there were multiple.

@[d;`col;{x^(enlist[`PIN]!enlist`YEN) x}]
date| 2022.12.01 2022.12.03
tab | `TRADE`SYM
col | `ID`CODE`YEN`NAME

Deletions could be done with except.

q)@[d;`col;except[;`PIN`CODE]]
date| 2022.12.01 2022.12.03
tab | TRADE      SYM
col | ID         NAME

I wouldn't be surprised to find better ways to do both these actions.

Upvotes: 4

Maurice Lim
Maurice Lim

Reputation: 883

You could do something like:

q)@[d;`col;{x where not x in`CODE`PIN}]
date| 2022.12.01 2022.12.03
tab | TRADE      SYM
col | ID         NAME

Upvotes: 4

Related Questions