2

Just curious what does this KX code do:

" " {`$first x vs y}/: column

it is part of the large KX query which is used after in the update statement.

What is x vs y and where they come from and what /: does together with " " on the left side?

the whole query looks like this:

select count i by date, somecolumn from update somecolumn:(" " {`$first x vs y}/: othercolumn) from select from some_table

What is x vs y and where they come from and what /: does together with " " on the left side?

2 Answers 2

3

/: is an adverb which means 'each right'.

One simple example is using the , with /: to join the left item to each of the right items:

q)1 2 3,/:4 5 6
1 2 3 4    / 1 2 3 join with 4
1 2 3 5    / 1 2 3 join with 5
1 2 3 6    / 1 2 3 join with 6

vs is 'vector scalar' which in your case separate strings from a delimiter " ".

q)" " vs "1 2 3"
,"1"
,"2"
,"3"

{`$first x vs y} is a function that cut y with x, take the first of the result and cast it to a symbol:

q){`$first x vs y}[" ";"1 2 3"]
`1

Combine them all together, " " {`$first x vs y}/: column means cut each of the strings in 'column' with " ", take the first of each results and cast them to symbols:

q)update res:" "{`$first x vs y}/:test from ([]test:("1 2 3";"4 5 6"))
test    res
-----------
"1 2 3" 1
"4 5 6" 4
Sign up to request clarification or add additional context in comments.

Comments

1

In simple words: It takes first value before space from a string and cast it to symbol.

Ex: Input "Hello World" will give output `Hello

 q> {`$first  x vs y}  [" ";"Hello World"]

Output: `Hello

You can test it with simple list (column in your query will be replaced by actual list from table corresponding to that column )

  q>  " "{`$first  x vs y}/: ("hello world" ; "test program")

output: hellotest

Comparing this to ex1, " " is moved to start which is due to syntax of "each right(/:)"

Alternate simpler version of this is:

   q){`$first   " "  vs x } each ("hello world" ; "test program")

WooiKent has already explained the functions. But here are some references:

Each-Right(/:) : It is a loop which iterates over values of right list

http://code.kx.com/q/ref/adverbs/#each-right

vs(Vector From Scalar): Break a string on some delimiter

http://code.kx.com/q/ref/casting/#vs

In your query, it takes a column whose type is string as input. For each element(row) of that column, takes first value before space. Finally cast that value to symbol.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.