Skip to content

Swiss army knife

vs

“Vector from scalar”

  • partition a symbol, string, or bytestream
  • encode a vector from an atom, or a matrix from a vector
x vs y    vs[x;y]

Partition

String by char

Where x is a char atom or string, and y is a string, returns a list of strings: y cut using x as the delimiter.

q)"," vs "one,two,three"
"one"
"two"
"three"
q)", " vs "spring, summer, autumn, winter"
"spring"
"summer"
"autumn"
"winter"
q)"|" vs "red|green||blue"
"red"
"green"
""
"blue"

String or bytestream by linebreak

Where x is the empty symbol `, and y is a string or bytestream, returns as a list of strings y partitioned on embedded line terminators into lines. (Recognizes both Unix \n and Windows \r\n terminators).

q)` vs "abc\ndef\nghi"
"abc"
"def"
"ghi"
q)` vs "x"$"abc\ndef\nghi"
"abc"
"def"
"ghi"
q)` vs "abc\r\ndef\r\nghi"
"abc"
"def"
"ghi"
Elides trailing linebreaks

The treatment of linebreaks varies usefully from a left argument of \n.

q)"\n" vs "abc\ndef\nghi\n"
"abc"
"def"
"ghi"
""
q)` vs "abc\ndef\nghi\n"
"abc"
"def"
"ghi"

Symbol by dot

Where x is the null symbol `, and y is a symbol, returns as a symbol vector y split on `.`.

q)` vs `mywork.dat
`mywork`dat

File handle

Where x is the empty symbol `, and y is a file handle, returns as a symbol vector y split into directory and file parts.

q)` vs `:/home/kdb/data/mywork.dat
`:/home/kdb/data`mywork.dat

sv join

Byte Vectors

Since 4.1t 2024.01.11, y can be a byte vector: y cut using x as the delimiter.

q)0x02 vs 0x0102010201
,0x01
,0x01
,0x01
q)0x0203 vs 0x000102030405
0x0001
0x0405
q)" "vs"x"$"a b"    / type inferred from left hand side
,"a"
,"b"

Encode

Bit representation

Where x is 0b and y is an integer, returns the bit representation of y.

q)0b vs 23173h
0101101010000101b
q)0b vs 23173
00000000000000000101101010000101b

Since 4.1t 2021.09.03, y also supports guids.

q)0b vs rand 0Ng
10001100011010111000101101100100011010000001010101100000100001000000101000111110000101111000010000000001001001010001101101101000b

Byte representation

Where x is 0x0 and y is a number, returns the internal representation of y, with each byte in hex.

q)0x0 vs 2413h
0x096d
q)0x0 vs 2413
0x0000096d
q)0x0 vs 2413e
0x4516d000
q)0x0 vs 2413f
0x40a2da0000000000
q)"."sv string"h"$0x0 vs .z.a / ip address string from .z.a
"192.168.1.213"

Base-x representation

Where x and y are integer, the result is the representation of y in base x. (Since V3.4t 2015.12.13.)

q)10 vs 1995
1 9 9 5
q)2 vs 9
1 0 0 1
q)24 60 60 vs 3805
1 3 25
q)"." sv string 256 vs .z.a / ip address string from .z.a
"192.168.1.213"

Where y is an integer vector the result is a matrix with count[x] items whose i-th column (x vs y)[;i] is identical to x vs y[i]. More generally, y can be any list of integers, and each item of the result is identical to y in structure.

q)a:10 vs 1995 1996 1997
q)a
1 1 1
9 9 9
9 9 9
5 6 7
q)a[;0]
1 9 9 5
q)10 vs(1995;1996 1997)
1 1 1
9 9 9
9 9 9
5 6 7

sv decode
.Q.j10 encode binhex, .Q.j12 encode base64
.Q.x10 decode binhex, .Q.x12 decode base64