Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

KDB; stripping characters from column of symbols

Any ideas how to do this nicely in KDB?

Consider the table

X:([]a:1 2 3;b:`abc11`abc22`abc33;c:10 20 30)

I now want a new table Y that has the "abc" stripped out of the symbols in the second column, such that:

Y:([]a:1 2 3;b:`11`22`33;c:10 20 30)
like image 281
bigO6377 Avatar asked Apr 21 '14 17:04

bigO6377


1 Answers

q)t:([]a:1 2 3;b:`abc11`abc22`abc33;c:10 20 30)
q)t
a b     c
----------
1 abc11 10
2 abc22 20
3 abc33 30

Strip the first three characters:

q)update `$3_'string b from t
a b    c
---------
1 11 10
2 22 20
3 33 30

Or search and replace:

q)update `$ssr[;"abc";""] each string b from t
a b    c
---------
1 11 10
2 22 20
3 33 30

If the table is large and has many repeating items, consider using .Q.fu:

q)t:1000000#([]a:1 2 3;b:`abc11`abc22`abc33;c:10 20 30)
q)\t r1:update `$3_'string b from t
111
q)\t r2:update .Q.fu[{`$3_'string x};b] from t
5
q)r1~r2
1b
like image 180
Ryan Hamilton Avatar answered Sep 22 '22 21:09

Ryan Hamilton