I have 2 table in kdb as below
q)table1:([]A:1 2 3 5 5 6 2 1;B:`HAK`ZAK`NAK`AAK`AZK`HAK`ZAK`HAK;C:2000.01.01+0 1 2 3 4 0 1 0)
q)table1
A B C
----------------
1 HAK 2000.01.01
2 ZAK 2000.01.02
3 NAK 2000.01.03
5 AAK 2000.01.04
5 AZK 2000.01.05
6 HAK 2000.01.01
2 ZAK 2000.01.02
1 HAK 2000.01.01
q)table2:([]B:`HAK`ZAK`NAK`AAK`AZK;Z:`NAFK`RFK`NAFK`RFK`ORQ)
q)table2
B Z
--------
HAK NAFK
ZAK RFK
NAK NAFK
AAK RFK
AZK ORQ
I want to modify Table1 column B as per mapping of Table 2. eg wherever in table1 columnB has word "HAK", then look in table2 columnB and replace table1 with respective table2 columnz Same for all rows for table1.
final output i want is table1 should be updated like below.
A B C
-----------------
1 NAFK 2000.01.01
2 RFK 2000.01.02
3 NAFK 2000.01.03
5 RFK 2000.01.04
5 ORQ 2000.01.05
6 NAFK 2000.01.01
2 RFK 2000.01.02
1 NAFK 2000.01.01
The function which I came up with is below.
hfun:
{$[
x in `$("HAK");`$("NAFK");
x in `$("ZAK");`$("RFK");
x in `$("NAK");`$("NAFK");
x in `$("AAK");`$("RFK");
x in `$(AZK);`$("ORQ");
x]}
finalOutput:update B:hfun'[B] from table1
The above function is working as expected but its not feasible to write every time a new function for new mappings or if table2 has 200 rows.
Can someone please take a look and advise further?