How do I replace items in a list in KDB?

Viewed 336

Let's say I have a list:

d: 0w 1 2 3

I'd like to find and replace the 0w in the list. I was thinking I could index on the list to find them, returning a list of booleans, with:

d . {x=0w}

but this didn't work.

What am I doing wrong? How do I replace my 0w's with something friendlier?

2 Answers

If you want to find the index of each 0w you could do something like this

where 0w=d

Then if you want to replace all instances of it in d with, say 4, you can do

d[where 0w=d]:4f

Just watch out for type errors when replacing the entry

For multiple replacements you could alternatively use a dictionary:

q)d:0W 1 2 3
q)remap:0W 2!0 22
q)d^remap d
0 1 22 3

remap 0W to 0 and remap 2 to 22

Related