Reference card

Reserved words

Aabs acos aj aj0 all and any asc asin asof atan attr avg avgs
Bbin binr
Cceiling cols cor cos count cov cross csv cut
Ddelete deltas desc dev differ distinct div do dsave
Eeach ej ema enlist eval except exec exit exp
Ffby fills first fkeys flip floor
Gget getenv group gtime
Hhclose hcount hdel hopen hsym
Iiasc idesc if ij ijf in insert inter inv
Kkey keys
Llast like lj ljf load log lower lsq ltime ltrim
Mmavg max maxs mcount md5 mdev med meta min mins mmax mmin mmu mod msum
Nneg next not null
Oor over
Pparse peach pj prd prds prev prior
Rrand rank ratios raze read0 read1 reciprocal reval reverse rload rotate rsave rtrim
Ssave scan scov sdev select set setenv show signum sin sqrt ss ssr string sublist sum sums sv (decode) sv (consolidate) svar system
Ttables tan til trim type
Uuj ujf ungroup union update upper upsert
Vvalue var view views vs (encode) vs (split)
Wwavg where while within wj wj1 wsum
Xxasc xbar xcol xcols xdesc xexp xgroup xkey xlog xprev xrank

.Q.id (purge), .Q.res (reserved words)

Iteration

f each xeachapply f to each item of x
x f'yeach-bothapply f between items of x and y
x f\:yeach-leftapply f between y and items of x
x f/:yeach-rightapply f between x and items of y
f':xeach-parallelapply f to items of x in parallel tasks
f peach xpeachapply f to items of x in parallel tasks
f':xeach-priorapply f between successive pairs of items of x
f prior xpriorapply f between successive pairs of items of x
x f/yrepeatapply f to y, x times
{x} f/yoverreduce y with f
f over xoverapplies f between successive items of x
f/[x;y;…]foldreduce [x;y;…] with f
f\xconvergeapply f to x until converges
x f\yiterateapply f to y, x times or while x
f\xscanapply f/ to successive items of x
f scan xscanapply f/ to successive items of x
do[x;e1;e2;e3;…;en]doevaluate e1 to en, x times
while[t;e1;e2;e3;…;en]whileevaluate e1 to en until t is false

Adverbs, Evaluation control

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.)

infixprefixpostfix
@index/applyamend, trap
.index/applytrap
?find, roll, deal, enum-extendvector conditional, exec, select
!dict, key, enumerate , ints to enumupdate, delete
$cast, tok, enum, pad, product, mmucond
:assign/amendreturn
'signal, composecase, each-both
\:int to byteeach-left
/:each-right
':each-parallel, each-prior
/(see note below)converge-repeat, over, fold
\(see note below)converge-iterate, scan

Other roles of /

  • in a session or script, preceded by a space, begins a trailing comment
  • in a script, followed by a newline, begins a multiline comment

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
csvcomma as record delimiter
-n!internal functions

Attributes

`s  sorted     `u  unique
`p  parted     `g  grouped

Attributes

Numbers

0:prepare-text, save-text, load-csv, load-fixed, parse-kv
1:read-binary, save-binary
2:dynamic load
0write to console
1 -1write to stdout
2 -2write to stderr
n, -nwrite to handle

Command-line options and system commands

file
\atables-bblocked
\b \Bviews\c -cconsole size
-C \CHTTP size\cdchange directory
\ddirectory\e -eerror traps
-ETLS Server Mode\ffunctions
\g -ggarbage collection\lload file or directory
-l -Llog sync\o -oUTC offset
\p -pmultithread port\P -Pdisplay precision
-qquiet mode\r -rreplicate
\rrename\s -sslaves
\Srandom seed\t -ttimer ticks
\tstime and space\T -Ttimeout
\u -u -Uusr-pwd-udisable syscmds
\vvariables\w -wmemory
\W -Wweek offset\xexpunge
\z -zdate format\1 \2redirect
\_hide q code\terminate
\toggle q/k\\quit

command-line options, system commands, OS Commands, system

Environment variables

vardefaultuse
QHOME$HOME/qfolder searched for q.k and unqualified script names
QLIC$HOMEfolder searched for k4.lic license file
QINITq.qadditional file loaded after q.k has initialised
LINESsupplied 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

ncnameszliteralnullinfSQLJava.Net
0*list
1bboolean10bBooleanboolean
2gguid160NgUUIDGUID
4xbyte10x00Bytebyte
5hshort20h0Nh0WhsmallintShortint16
6iint40i0Ni0WiintIntegerint32
7jlong80j or 00Nj
or 0N
0Wj
or 0W
bigintLongint64
8ereal40e0Ne0WerealFloatsingle
9ffloat80.0 or 0f0n0wfloatDoubledouble
10cchar1" "" "Characterchar
11ssymbol.``varcharStringstring
12ptimestamp8dateDtimespan0Np0WpTimestampDateTime (RW)
13mmonth42000.01m0Nm
14ddate42000.01.010Nd0WddateDate
15zdatetime8dateTtime0Nz0wztimestampTimestampDateTime (RO)
16ntimespan800:00:00.0000000000Nn0WnTimespanTimeSpan
17uminute400:000Nu0Wu
18vsecond400:00:000Nv0Nv
19ttime400:00:00.0000Nt0WttimeTimeTimeSpan
20-76enums
77(unused)
78-9677+t – mapped list of lists of type t
97nested sym enum
98table
99dictionary
100lambda
101unary primitive
102operator
103adverb
104projection
105composition
106f'
107f/
108f\
109f':
110f/:
111f\:
112dynamic 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 char$data:CHAR$string

date.(year month week mm dd)
dict:`a`b!…
table:([]x:…;y:…) or +`x`y!…
time.(minute second mm ss)
milliseconds: time mod 1000

Namespaces

.z

.z.aIP address.z.acHTTP auth from cookie
.z.bdependencies.z.bmmsg validator
.z.ccores.z.eTLS connection status
.z.exitaction on exit.z.ffile
.z.hhost.z.iPID
.z.krelease date.z.Kversion
.z.llicense.z.nUTC timespan
.z.Nlocal timespan.z.oOS version
.z.pUTC timestamp.z.Plocal timestamp
.z.pcclose.z.pdpeach handles
.z.pgget.z.phHTTP get
.z.piinput.z.pmHTTP options
.z.poopen.z.ppHTTP post
.z.psset.z.pwvalidate user
.z.qquiet mode.z.sself
.z.tstimer.z.uuser ID
.z.vsvalue set.z.whandle
.z.Whandles.z.wcwebsocket close
.z.wowebsocket open.z.wswebsockets
.z.xargv.z.Xraw command line
.z.zUTC datetime.z.Zlocal datetime
.z.zdzip defaults
.z.t, .z.T, .z.d, .z.Dtime/date shortcuts

.h

.h.brlinebreak.h.c0web color
.h.c1web color.h.cdCSV from data
.h.code.h.data
.h.edExcel from data.h.edsn
.h.framframe.h.haanchor
.h.hbanchor target.h.hcescape lt
.h.heHTTP 400.h.hnHTTP error
.h.hpHTTP response.h.hrhorizontal rule
.h.htMarqdown to HTML .h.htastart tag
.h.htacelement.h.htcelement
.h.htmldocument.h.httphyperlinks
.h.huURI escape.h.hugURI map
.h.hyHTTP response.h.iso8601ISO timestamp
.h.jxtable.h.logoKx logo
.h.nbrno break.h.prepre
.h.sastyle.h.sbstyle
.h.scURI-safe.h.tdTSV
.h.textparagraphs.h.txfiletypes
.h.tyMIME types.h.uhURI unescape
.h.xdXML.h.xmpxmp
.h.xsXML escape.h.xtJSON

.Q

.Q.addmonths.Q.addrIP address
.Q.bvbuild vp.Q.chkfill HDB
.Q.cncount partitioned table.Q.Dpartitions
.Q.ddjoin symbols.Q.def
.Q.dpftsave table.Q.dsftgload process save
.Q.enenumerate varchar cols.Q.fformat
.Q.fcparallel on cut .Q.ffappend columns
.Q.fkforeign key.Q.fmtformat
.Q.fpsstreaming algorithm.Q.fsstreaming algorithm
.Q.fsnstreaming algorithm.Q.ftapply simple
.Q.fuapply unique.Q.gcgarbage collect
.Q.hdpfsave tables.Q.hgHTTP get
.Q.hosthostname.Q.hpHTTP post
.Q.idpurge.Q.indpartitioned index
.Q.j10encode binhex .Q.j12encode base64
.Q.kversion .Q.lload
.Q.Mlong infinity.Q.MAPmaps partitions
.Q.optcommand parameters.Q.Psegments
.Q.parlocate partition.Q.pdmodified partition locations
.Q.PDpartition locations.Q.pfpartition type
.Q.pnpartition counts.Q.ptpartitioned tables
.Q.pvmodified partition values.Q.PVpartition values
.Q.qpis partitioned.Q.qtis table
.Q.resk words.Q.splain text
.Q.tytype.Q.udate based
.Q.Vtable to dict.Q.vvalue
.Q.viewsubview.Q.vpmissing partitions
.Q.wmemory stats.Q.x10decode binhex
.Q.x12decode base64 .Q.xnon-command parameters
.Q.Xfcreate file

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.