0

I would like to update a table by reference without getting an assign error in kdb i.e.

q)tab:1!(flip`a`b`c!(til 10;10?50;10?100));
q)k:2#key[tab];
q)(`tab[k;`b`c])*:2;
parse error
assign

or

 q)[.:][`tab][k;`b`c]*:2;
    parse error
    assign

(obviously one cannot assign a value to a derived variable)

Notably the following work:

q)(tab[k;`b`c])*:2;

I've made the assumption here that there are better methods to achieve this than using eval or value[`tab] I was hoping I could get a second opinion to that end. How might one implement the set by reference as shown in the example above with a "symbolic" reference to a table? Thanks again, best regards.

1 Answers1

1

What you said "works" doesn't:

q)tab[k;`b`c]:0 1;
'length
  [0]  tab[k;`b`c]:0 1;
                  ^

you must have been working with an unkeyed table rather than the keyed table.

Ultimately it comes down to whether the table is keyed or not and what you're using to index into the table (keys or indices).

Keyed table - key lookup

q)tab:1!(flip`a`b`c!(100+til 10;10?50;10?100))
q)k:100 101
q).[`tab;(([]a:k);`b);:;0 1]

Unkeyed table - index lookup

q)tab:(flip`a`b`c!(100+til 10;10?50;10?100))
q)k:0 1
q).[`tab;(k;`b);:;0 1]
terrylynch
  • 11,844
  • 13
  • 21