AFox
AFox

Reputation: 97

How to update multiple tables in kdb

Say I have a list of tables. (sym1, sym2, sym3 etc)

How would I add a new column to each table called Sym containing the table name?

Thank you

Upvotes: 1

Views: 254

Answers (2)

nyi
nyi

Reputation: 3229

Another way to achieve this :

q){update Sym:x from x}each `sym1`sym2`sym3
q)raze (sym1;sym2;sym3)
p        s  Sym
----------------
2.08725  75 sym1
2.065687 6  sym1
2.058972 63 sym2
2.095509 62 sym2
2.036151 90 sym3
2.090895 63 sym3

If you are getting these tables (sym1,sym2,sym3) as the output of another function call like :

f each `s1`s2`s3

then I'll suggest updating the function to add the column Sym just before return these individual tables.

f:{
   /some logic
   update Sym:x from t
  }

This will save an operation of adding a new column separately

Upvotes: 0

James Burrows
James Burrows

Reputation: 31

You could use something like:

q){@[value x;`Sym;:;x]}each tables[]
+`a`b`c`Sym!(0 1 2 3 4;0 1 2 3 4;0 1 2 3 4;`sym1`sym1`sym1`sym1`sym1)
+`a`b`c`Sym!(0 1 2 3 4;0 1 2 3 4;0 1 2 3 4;`sym2`sym2`sym2`sym2`sym2)
+`a`b`c`Sym!(0 1 2 3 4;0 1 2 3 4;0 1 2 3 4;`sym3`sym3`sym3`sym3`sym3)

If you remove value from the first argument of @, this will update the tables in place.

Otherwise, since this returns a list, you can use indexing to return the table you want from the list:

q)({@[value x;`Sym;:;x]}each tables[])0
a b c Sym
----------
0 0 0 sym1
1 1 1 sym1
2 2 2 sym1
3 3 3 sym1
4 4 4 sym1

Hope this helps,

James

Upvotes: 2

Related Questions