How to join strings in a table in kdb?

2019-09-22 03:54发布

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

标签: kdb
3条回答
霸刀☆藐视天下
2楼-- · 2019-09-22 03:59

Your query gives you a length error:

q)tab:([]service:`CS`CS.US`CS.US_ABC;y:1 2 3)
q)update service:`$(((string[service],'(":"))),'( "primary")) from tab
'length
  [0]  update service:`$(((string[service],'(":"))),'( "primary")) from tab
                                                    ^

This happens because ,' (concatenate each) expects vectors of equal length on both sides, but gets a table column size (3) vector on the left and a character vector of length 7 on the right. Notice what happens when you pass 3 characters:

q)update service:`$(((string[service],'(":"))),'( "pri")) from tab
service     y
-------------
CS:p        1
CS.US:r     2
CS.US_ABC:i 3

Each row gets a different suffix. What you want is to use ,\: (concatenate each-left):

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

Why does it work for "0"? It works because "0" is not a vector but a character scalar

q)type "0"
-10h
q)type "primary"
10h

and with a scalar on the right, ,' works the same as .\::

q)"ab",'"0"
"a0"
"b0"
q)"ab",\:"0"
"a0"
"b0"

Finally, your query will run faster if you first prepend ":" to the suffix and then append the result to each service:

q)update service:`$(string[service],\:":","primary") from tab
service           y
-------------------
CS:primary        1
CS.US:primary     2
CS.US_ABC:primary 3 
查看更多
Viruses.
3楼-- · 2019-09-22 04:09
  q)update service:`$(((string[service],'(":"))),'(count[i]#enlist "primary")) 
  from tab
  service           y
  -------------------
  CS:primary        1
  CS.US:primary     2
  CS.US_ABC:primary 3
查看更多
别忘想泡老子
4楼-- · 2019-09-22 04:24

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
查看更多
登录 后发表回答