KDB: Loop over 2 lists and filter/replace

Viewed 135

In KDB+ I have 2 lists of equal size, where I want to replace(probably use ssr ) strings in list1 conditioned on the values in list2.

list1:("Boy";"Toy";"Coy";"Poy")
list2:("A","B","A","B")

If list2[i]=="B" replace "oy" by "ab" So we should finally get

list1:("Boy";"Tab";"Coy";"Pab")
2 Answers
q)list1:("Boy";"Toy";"Coy";"Poy");
q)list2:"ABAB";
q)@[list1;where list2="B";ssr[;"oy";"ab"]]
"Boy"
"Tab"
"Coy"
"Pab"

The code below should achieve what you want:

{@[`list1;x;{(-2_x),"ab"}]} where list2="B"

Output:

q){@[`list1;x;{(-2_x),"ab"}]} where list2="B"
`list1
q)list1
"Boy"
"Tab"
"Coy"
"Pab"
Related