Search code examples
insert-updatekdb+q-lang

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.


Solution

  • 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