Reference card
Reserved words¶
| A | abs acos aj aj0 all and any asc asin asof atan attr avg avgs |
| B | bin binr |
| C | ceiling cols cor cos count cov cross csv cut |
| D | delete deltas desc dev differ distinct div do dsave |
| E | each ej ema enlist eval except exec exit exp |
| F | fby fills first fkeys flip floor |
| G | get getenv group gtime |
| H | hclose hcount hdel hopen hsym |
| I | iasc idesc if ij ijf in insert inter inv |
| K | key keys |
| L | last like lj ljf load log lower lsq ltime ltrim |
| M | mavg max maxs mcount md5 mdev med meta min mins mmax mmin mmu mod msum |
| N | neg next not null |
| O | or over |
| P | parse peach pj prd prds prev prior |
| R | rand rank ratios raze read0 read1 reciprocal reval reverse rload rotate rsave rtrim |
| S | save scan scov sdev select set setenv show signum sin sqrt ss ssr string sublist sum sums sv (decode) sv (consolidate) svar system |
| T | tables tan til trim type |
| U | uj ujf ungroup union update upper upsert |
| V | value var view views vs (encode) vs (split) |
| W | wavg where while within wj wj1 wsum |
| X | xasc xbar xcol xcols xdesc xexp xgroup xkey xlog xprev xrank |
.Q.id (purge), .Q.res (reserved words)
Iteration¶
f each x | each | apply f to each item of x |
x f'y | each-both | apply f between items of x and y |
x f\:y | each-left | apply f between y and items of x |
x f/:y | each-right | apply f between x and items of y |
f':x | each-parallel | apply f to items of x in parallel tasks |
f peach x | peach | apply f to items of x in parallel tasks |
f':x | each-prior | apply f between successive pairs of items of x |
f prior x | prior | apply f between successive pairs of items of x |
x f/y | repeat | apply f to y, x times |
{x} f/y | over | reduce y with f |
f over x | over | applies f between successive items of x |
f/[x;y;…] | fold | reduce [x;y;…] with f |
f\x | converge | apply f to x until converges |
x f\y | iterate | apply f to y, x times or while x |
f\x | scan | apply f/ to successive items of x |
f scan x | scan | apply f/ to successive items of x |
do[x;e1;e2;e3;…;en] | do | evaluate e1 to en, x times |
while[t;e1;e2;e3;…;en] | while | evaluate e1 to en until t is false |
Glyphs¶
Punctuation¶
() | precedence; list definition; table definition |
[] | expression block; arguments; table definition |
{} | function definition (lambda) |
; | separator |
` | symbol |
`: | filepath |
Operators¶
= | equal | <> | not equal | ~ | match | ||
< | less than | <= | less than or equal | > | greater than | >= | greater than or equal |
+ | plus | - | minus | * | times | % | divided by |
# | set attribute, take | , | join | ^ | fill, coalesce | _ | drop; cut |
| | maximum, or | & | minimum, and |
Overloads¶
The following glyphs denote operators, functions or adverbs – according to use. (Operators are listed here with infix syntax but have the same semantics in prefix syntax.)
| infix | prefix | postfix | |
|---|---|---|---|
@ | index/apply | amend, trap | |
. | index/apply | trap | |
? | find, roll, deal, enum-extend | vector conditional, exec, select | |
! | dict, key, enumerate , ints to enum | update, delete | |
$ | cast, tok, enum, pad, product, mmu | cond | |
: | assign/amend | return | |
' | signal, compose | case, each-both | |
\: | int to byte | each-left | |
/: | each-right | ||
': | each-parallel, each-prior | ||
/ | (see note below) | converge-repeat, over, fold | |
\ | (see note below) | converge-iterate, scan |
Other roles of /
Other roles of \
- as a command in the debugger, aborts evaluation and returns to the q session
- as a command in the session,
\toggles between the q and k parsers - as punctuation in a script, ends a multi-line comment
- as punctuation in a script, (on a line by itself) marks the end of executable program
Mavericks¶
:: | null, identity |
csv | comma as record delimiter |
-n! | internal functions |
Attributes¶
`s sorted `u unique
`p parted `g grouped
Numbers¶
0: | prepare-text, save-text, load-csv, load-fixed, parse-kv |
1: | read-binary, save-binary |
2: | dynamic load |
0 | write to console |
1 -1 | write to stdout |
2 -2 | write to stderr |
n, -n | write to handle |
Command-line options and system commands¶
| file | |||
\a | tables | -b | blocked |
\b \B | views | \c -c | console size |
-C \C | HTTP size | \cd | change directory |
\d | directory | \e -e | error traps |
-E | TLS Server Mode | \f | functions |
\g -g | garbage collection | \l | load file or directory |
-l -L | log sync | \o -o | UTC offset |
\p -p | multithread port | \P -P | display precision |
-q | quiet mode | \r -r | replicate |
\r | rename | \s -s | slaves |
\S | random seed | \t -t | timer ticks |
\ts | time and space | \T -T | timeout |
\u -u -U | usr-pwd | -u | disable syscmds |
\v | variables | \w -w | memory |
\W -W | week offset | \x | expunge |
\z -z | date format | \1 \2 | redirect |
\_ | hide q code | \ | terminate |
\ | toggle q/k | \\ | quit |
command-line options, system commands, OS Commands, system
Environment variables¶
| var | default | use |
|---|---|---|
QHOME | $HOME/q | folder searched for q.k and unqualified script names |
QLIC | $HOME | folder searched for k4.lic license file |
QINIT | q.q | additional file loaded after q.k has initialised |
LINES | supplied by OS, used to set \c | |
COLUMNS | \c $LINES $COLUMNS |
If not set, LINES COLUMNS default to 25 80 for console, and 36 2000 for Web. \c clamps to range 10…2000 for both inputs.
Ensure LINES and COLUMNS are exported. In Bash
$ export LINES COLUMNS
before starting q.
q)getenv`VARNAME
q)`VARNAME setenv "NEWVALUE"
Datatypes¶
| n | c | name | sz | literal | null | inf | SQL | Java | .Net | ||
|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | * | list | |||||||||
| 1 | b | boolean | 1 | 0b | Boolean | boolean | |||||
| 2 | g | guid | 16 | 0Ng | UUID | GUID | |||||
| 4 | x | byte | 1 | 0x00 | Byte | byte | |||||
| 5 | h | short | 2 | 0h | 0Nh | 0Wh | smallint | Short | int16 | ||
| 6 | i | int | 4 | 0i | 0Ni | 0Wi | int | Integer | int32 | ||
| 7 | j | long | 8 | 0j or 0 | 0Njor 0N | 0Wjor 0W | bigint | Long | int64 | ||
| 8 | e | real | 4 | 0e | 0Ne | 0We | real | Float | single | ||
| 9 | f | float | 8 | 0.0 or 0f | 0n | 0w | float | Double | double | ||
| 10 | c | char | 1 | " " | " " | Character | char | ||||
| 11 | s | symbol | . | ` | ` | varchar | String | string | |||
| 12 | p | timestamp | 8 | dateDtimespan | 0Np | 0Wp | Timestamp | DateTime (RW) | |||
| 13 | m | month | 4 | 2000.01m | 0Nm | ||||||
| 14 | d | date | 4 | 2000.01.01 | 0Nd | 0Wd | date | Date | |||
| 15 | z | datetime | 8 | dateTtime | 0Nz | 0wz | timestamp | Timestamp | DateTime (RO) | ||
| 16 | n | timespan | 8 | 00:00:00.000000000 | 0Nn | 0Wn | Timespan | TimeSpan | |||
| 17 | u | minute | 4 | 00:00 | 0Nu | 0Wu | |||||
| 18 | v | second | 4 | 00:00:00 | 0Nv | 0Nv | |||||
| 19 | t | time | 4 | 00:00:00.000 | 0Nt | 0Wt | time | Time | TimeSpan | ||
| 20-76 | enums | ||||||||||
| 77 | anymap | ||||||||||
| 78-96 | 77+t – mapped list of lists of type t | ||||||||||
| 97 | nested sym enum | ||||||||||
| 98 | table | ||||||||||
| 99 | dictionary | ||||||||||
| 100 | lambda | ||||||||||
| 101 | unary primitive | ||||||||||
| 102 | operator | ||||||||||
| 103 | adverb | ||||||||||
| 104 | projection | ||||||||||
| 105 | composition | ||||||||||
| 106 | f' | ||||||||||
| 107 | f/ | ||||||||||
| 108 | f\ | ||||||||||
| 109 | f': | ||||||||||
| 110 | f/: | ||||||||||
| 111 | f\: | ||||||||||
| 112 | dynamic load | ||||||||||
n: short int returned by type and used for casting, e.g. 9h$3
c: character used lower-case for casting and upper-case for load-csv
sz: size in bytes
inf: infinity (no math on temporal types); 0Wh is 32767h
RO: read only; RW: read-write
Nested types are 77+t (e.g. 78 is boolean. 96 is time.)
Cast and tok char$data:CHAR$string
date.(year month week mm dd)
time.(hh minute second uu ss)
milliseconds: "i"$time mod 1000
timestamp.(year month week mm dd hh minute uu ss)
nanoseconds: "i"$timestamp mod 1000000000
datetime.(year month week mm dd hh minute uu ss)
milliseconds: "i"$mod[;1000]"t"$datetime
dict:`a`b!…
table:([]x:…;y:…) or (+)`x`y!…
Namespaces¶
.z¶
.h¶
.Q¶
Single-letter namespaces
All single-letter namespaces (upper- and lower-case) are reserved for Kx use. Do not use single-letter namespaces as containers for user-written code or data.