Simplifying multiple update statements – KDB

  • A+
Category:Languages

I have a table that contains multiple flags, I want to map each of these flags to the values they represent.

tab:([] t:til 4 ; f1:1100b;f2:1010b;f3:0101b;f4:0011b ) 

How to simplify these multiple update statements?

tab:update f1s:`googl from tab where f1 tab:update f2s:`appl from tab where f2 tab:update f3s:`amzn from tab where f3 

 


There can also be achieved by using dictionaries :

tab:([] t:til 4 ; f1:1100b;f2:1010b;f3:0101b;f4:0011b ) g:01b!``googl  ap:01b!``appl  az:01b!``amzn  update f1s:g@f1, f2s:ap@f2, f3s:az@f3 from tab 

This is particularly useful in cases when there are multiple values in a single column :

q)tab:([] t:til 4 ; f1:1 2 0 4;f2:2 3 4 1;f3:1 4 3 1 ;f4:1 1 0 0 ) q)m:1 2 3!`googl`appl`amzn  q)update f1s:m@f1, f2s:m@f2, f3s:m@f3 from tab  t f1 f2 f3 f4 f1s   f2s   f3s ------------------------------- 0 1  2  1  1  googl appl  googl 1 2  3  4  1  appl  amzn 2 0  4  3  0              amzn 3 4  1  1  0        googl googl 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: