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 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 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#attribute
`symbol`:filepath

Operators

=equal<>not equal~match
<less than<=less than or equal>greater than>=greater than or equal
+plus-minus*times%divided by
#take,join^fill, coalesce_drop; cut
|maximum, or&minimum, and

Overloads

The following glyphs represent operators, functions or adverbs – according to use.

infixprefixpostfix
'casesignalcompose, each-both
@index/applyamendtrap
.index/apply
?find, roll, deal, enum-extendvector conditional, exec, select
!dict, key, enumerate , ints to enumupdate, delete
$cast, tok, enum, pad, product, mmucond
:assign/amendreturn
\: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

Attributes

exampleoverhead
`s#2 2 3sorted0
`u#2 4 5unique16*u
`p#2 2 1parted(4*u;16*u;4*u+1)
`g#2 1 2grouped(4*u;16*u;4*u+1;4*n)

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

\atables-bblocked
\b \Bviews\c -cconsole size
-C \CHTTP size\cdchange directory
\ddirectory\e -eerror traps
\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
\\quitOS Commands
systemfile

command-line options, system commands

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

Ensure LINES and COLUMNS are exported. In Bash

$ export LINES COLUMNS
before starting q.
q)getenv`VARNAME
q)`VARNAME setenv "NEWVALUE"

Datatypes

charsizenumliteralnullnameSQLJava.Net
b110bbooleanBooleanboolean
g1620NgguidUUIDGUID
x140x00byteBytebyte
h250h0NhshortsmallintShortint16
i460i0NiintintIntegerint32
j870j or 00Nj or 0NlongbigintLongint64
e480e0NerealrealFloatsingle
f890.0 or 0f0nfloatfloatDoubledouble
c110" "" "charCharacterchar
s.11``symbolvarcharStringstring
p812dateDtimespan0NptimestampTimestampDateTime (r/w)
m4132000.01m0Nmmonth
d4142000.01.010NddatedateDate
z815dateTtime0NzdatetimetimestampTimestampDateTime (read only)
n81600:00:00.0000000000NntimespanTimespanTimeSpan
u41700:000Numinute
v41800:00:000Nvsecond
t41900:00:00.0000NttimetimeTimeTimeSpan

Nested types are 77+t (e.g. 78 is boolean. 96 is time.)

Cast char$data:CHAR$string

Int, float, char, symbol, literal nulls: 0N 0n " ", `.
Rest use type extensions, e.g. 0Nd. No boolean or byte nulls.

Infinite dates, times, datetimes: 0Wd 0Wz 0Wt (No math.)

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.