$
Tok¶
Interpret a string as a data value
x$y $[x;y]
Where
y
is a stringx
is a non-positive short or upper-case char as below (or the null symbol as a synonym for"S"
)
returns y
as an atom value interpreted according to x
.
x
values for Tok:
q){([result:key'[x$\:()]];short:neg x;char:upper .Q.t x)}5h$where" "<>20#.Q.t
result | short char
---------| ----------
string | 0 *
boolean | -1 B
guid | -2 G
byte | -4 X
short | -5 H
int | -6 I
long | -7 J
real | -8 E
float | -9 F
char | -10 C
symbol | -11 S
timestamp| -12 P
month | -13 M
date | -14 D
datetime | -15 Z
timespan | -16 N
minute | -17 U
second | -18 V
time | -19 T
A left argument of 0h
or "*"
returns the y
string unchanged.
Where x
is a positive or zero short, a lower-case char, "*"
, or a non-null symbol, see Cast.
q)"E"$"3.14"
3.14e
q)-8h$"3.14"
3.14e
q)"D"$"2000-12-12"
2000.12.12
q)"U"$"12:13:14"
12:13
q)"T"$"123456789"
12:34:56.789
q)"P"$"2015-10-28D03:55:58.6542"
2015.10.28D03:55:58.654200000
Outside of domain¶
Parsing values outside of the types domain returns null.
q)"H"$"32768"
0Nh
q)"I"$"2147483648"
0Ni
q)"D"$"2147483648"
0Nd
Changes since 4.1t 2021.09.03,4.0 2021.10.01
Short converts to 0Nh instead of ±0Wh
Iteration¶
Tok is a near-atomic function. Implicit recursion stops at strings, not atoms.
q)"BXH"$("42";"42";"42")
0b
0x42
42h
q)("B";"XHI")$("42";("42";"42";"42"))
0b
(0x42;42h;42i)
q)"B"$" Y "
1b
q)"B"$'" Y "
000100b
Symbols¶
Use the null symbol as a shorthand left argument for "S"
.
q)"S"$"hello"
`hello
q)`$"hello"
`hello
Converting a string to a symbol removes leading and trailing blanks.
q)`$" IBM "
`IBM
Truthy characters¶
Certain characters are recognized as boolean True:
q)"B"$(" Y ";" N ")
10b
q)" ",.Q.an
" abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789"
q)"B"$'" ",.Q.an
0000000000000000000010001100000000000000000000100011000100000000b
q).Q.an where"B"$'.Q.an
"txyTXY1"
Contrast this with casting to boolean:
q)"b"$" ",.Q.an
1111111111111111111111111111111111111111111111111111111111111111b
IP address¶
q)"I"$"192.168.1.34" /an IP address as an int
-1062731486i
q)"NT"$\:"123456123987654" / since V3.4
0D12:34:56.123987654
12:34:56.123
Unix timestamps¶
(from seconds since Unix epoch), string with 9…11 digits:
q)"P"$"10129708800"
2290.12.31D00:00:00.000000000
q)"P"$"00000000000"
1970.01.01D00:00:00.000000000
If these digits are followed by a .
Tok will parse what follows .
as parts of second, e.g.
q)"P"$"10129708800.123456789"
2290.12.31D00:00:00.123456789
q)"P"$"00000000000.123456789"
1970.01.01D00:00:00.123456789
q)"PZ"$\:"20191122-11:11:11.123"
2019.11.22D11:11:11.123000000
2019.11.22T11:11:11.123
Date formats¶
"D"$
will Tok dates with varied formats:
[yy]yymmdd
ddMMM[yy]yy
yyyy/[mm|MMM]/dd
[mm|MMM]/dd/[yy]yy / \z 0
dd/[mm|MMM]/[yy]yy / \z 1
Command-line option -z
(date format)
System command \z
(date format)
Cast
Overloads of $
.h.iso8601
ISO 8601 timestamp
Casting
Q for Mortals
§7.3.3 Parsing Data from Strings