Search code examples
kdb

Splitting a list of strings using cut - KDB


For the following list:

q)a:("ua@1100@1";"sba@2220@2";"r@4444@a")

I want following output :

("1100@1";"2220@2";"4444@a")

? gives first index of @

q)(a?\:"@")
2 3 1`

but using cut does not give the desired result :

q)(a?\:"@")cut'a
(("ua";"@1";"10";"0@";"1");("sba";"@22";"20@";"2");("r";"@";"4";"4";"4";"4";"@";"a"))`

Solution

  • You can also parse the data rather than drop chars from each string. It'll be somewhat more efficient if your dataset is large.

    q)("J@*"0:/:a)[;1]
    "1100@1"
    "2220@2"
    "4444@a"
    

    Notice I've set the 'key' to 'J' which will result in nulls in your example case, but you only care about the values anyway.

    If you can join (sv) the strings together, it'll be even better too

    q)last "J@;"0:";" sv a
    "1100@1"
    "2220@2"
    "4444@a"
    

    HTH, Sean