sv

“Scalar from vector”

  • join strings or filepath elements
  • decode a vector to an atom

Syntax: x sv y, sv[x;y]

Join

Strings

Where

  • y is a list of strings
  • x is a char atom, string, or the empty symbol

returns the strings in y, separated by x.

Where x is the empty symbol `, the strings are separated by the host line separator: \n on Unix, \r\n on Windows.

q)"," sv ("one";"two";"three")    / comma-separated
"one,two,three"
q)"\t" sv ("one";"two";"three")   / tab-separated
"one\ttwo\tthree"
q)", " sv ("one";"two";"three")   / x may be a string
"one, two, three"
q)"." sv string 192 168 1 23      / form IP address
"192.168.1.23"
q)` sv ("one";"two";"three")      / use host line separator
"one\ntwo\nthree\n"

Filepath components

Where

  • x is the empty symbol `
  • y is a symbol list of which the first item is a file handle

returns a file handle where the items of the list are joined, separated by slashes. (This is useful when building file paths.)

q)` sv `:/home/kdb/q`data`2010.03.22`trade
`:/home/kdb/q/data/2010.03.22/trade

If the first item is not a file handle, returns a symbol where the items are joined, separated by . (dot). This is useful for building filenames with a given extension:

q)` sv `mywork`dat
`mywork.dat

vs partition

Decode

Base to integer

Where x and y are numeric atoms or lists, y is evaluated to base x.

q)10 sv 2 3 5 7
2357
q)100 sv 2010 3 17
20100317
q)0 24 60 60 sv 2 3 5 7   / 2 days, 3 hours, 5 minutes, 7 seconds
183907

When x is a list, the first number is not used. The calculation is done as:

q)baseval:{y wsum reverse prds 1,reverse 1_x}
q)baseval[0 24 60 60;2 3 5 7]
183907f

Bytes to integer

Where

  • x is 0x0
  • y is a vector of bytes of length 2, 4 or 8

returns y converted to the corresponding integer.

q)0x0 sv "x" $0 255           / short
255h
q)0x0 sv "x" $128 255
-32513h
q)0x0 sv "x" $0 64 128 255    / int
4227327
q)0x0 sv "x" $til 8           / long
283686952306183j
q)256j sv til 8               / same calculation
283686952306183j

Converting non-integers

Use File Binary – e.g.:


q)show a:0x0 vs 3.1415
0x400921cac083126f
q)(enlist 8;enlist "f")1: a   /float
3.1415

Bits to integer

Where

  • x is 0b
  • y is a boolean vector of length 8, 16, 32, or 64

returns y converted to the corresponding integer or (in the case of 8 bits) a byte value.

q)0b sv 64#1b
-1
q)0b sv 32#1b
-1i
q)0b sv 16#1b
-1h
q)0b sv 8#1b
0xff

vs encode
.Q.j10 (encode binhex)
.Q.x10 (decode binhex)
.Q.j12 (encode base64)
.Q.x12 (decode base64)