Search code examples
kdb

How to join strings in a table in kdb?


I would like to join string in kdb but didn't work well. This is the data:

tab:([]service:`CS`CS.US`CS.US_ABC;y:1 2 3)


`CS 1
`CS.US  2
`CS.US_ABC  3

I would like to add :0 and :primary depending on the given parameter. 0 is working now

update service:`$(((string[service],'(":"))),'("C"$string 0)) from tab

If I would like the data to become

`CS:primary 1
`CS.US:primary  2
`CS.US_ABC:primary  3

and the primary is either string or symbol, how could I join?

I am parameterizing the 0 and primary.

Currently, 0 works as follows

update service:`$(((string[service],'(":"))),'( "0")) from tab

but "primary" is not working

update service:`$(((string[service],'(":"))),'( "primary")) from tab

Solution

  • If you want primary to be a parameter rather than a fixed string, the following will work (primary is "no" in this example):

    q)update {`$string[y],\:":",x}[primary;]service from tab
    service      y
    --------------
    CS:no        1
    CS.US:no     2
    CS.US_ABC:no 3
    

    If primary is a fixed string then you can place it inside the lambda in lieu of "x" and replace "y" with "x", yielding the following:

    q)update {`$string[x],\:":","primary"}service from tab
    service           y
    -------------------
    CS:primary        1
    CS.US:primary     2
    CS.US_ABC:primary 3