Alim Hasanov
Alim Hasanov

Reputation: 307

kdb/q update a keyed table element's list element

I have a table like:

q)tbl[`XXX]
1977 1987 1997

and I want to update the nth element from the list of years, so the above becomes

q)tbl[`XXX]
1997 1987 2007

And need it to be inplace? Been looking into the docs but having hard figuring it out.

Upvotes: 1

Views: 1185

Answers (2)

emc211
emc211

Reputation: 1379

qsql version of Jamies answer:

q)update XXX:2007 from `tbl where i=n

This will also allows you to use where clause based on other columns in the table

Upvotes: 2

jomahony
jomahony

Reputation: 1702

You can think of the table as a flipped dictionary. To do a dictionary update, you index in on key first:

  q)tbl:([]XXX:1977 1987 1997)
  q)dict:flip tbl
  q)dict[`XXX;n]:2007
  q)dict
  XXX| 1977 1987 2007

Therefore to do an inplace update on a table the following syntax is used:

  q)tbl:([]XXX:1977 1987 1997)
  q)tbl[`XXX]
  1977 1987 1997
  q)tbl[n;`XXX]:2007
  q)tbl[`XXX]
  1977 1987 2007

Upvotes: 5

Related Questions