Skip to content

key

key x     key[x]

Key of a dictionary

Where x is a dictionary (or the name of one), returns its key.

q)D:`q`w`e!(1 2;3 4;5 6)
q)key D
`q`w`e
q)key `D
`q`w`e

A namespace is a dictionary, so key on the name of a namespace returns the names defined in it.

q)5#key `.q
``neg`not`null`string
q)key `.                / objects in the default namespace
,`D
q)key `                 / namespaces
``z`q`Q`h`j`s

The namespaces listed depend on the modules and scripts loaded in the session.

Keys of a keyed table

Where x is a keyed table (or the name of one), returns its key columns.

q)K:([s:`q`w`e]g:1 2 3;h:4 5 6)
q)key K
s
-
q
w
e

keys, xkey

! Enkey, Unkey

Files in a directory

Where x is a directory handle, returns a list of the objects in the directory, sorted ascending.

q)`:db/a.q`:db/b.q`:db/c.txt 0:\:enlist""
`:db/a.q`:db/b.q`:db/c.txt
q)key`:db
`s#`a.q`b.q`c.txt

To select particular files, use like.

q)f:key`:db
q)f where f like "*.q"
`a.q`b.q

Whether a file or directory exists

Where x is a file handle, returns x if the file exists, otherwise an empty list.

q)key`:db/a.q
`:db/a.q
q)()~key`:db/notfound.q
1b

An empty directory returns an empty symbol vector; a non-existent directory returns an empty general list.

q)()~key`:foo
1b
q)system"mkdir foo"
q)key`:foo
`s#`symbol$()

Whether a name is defined

Where x is a symbol atom that is neither a file or directory handle nor the name of a dictionary or keyed table, returns the original symbol if a variable of that name exists, otherwise an empty list. q interprets the name relative to the current context unless the name is fully qualified.

q)()~key`a        /now you don't see it
1b
q)a:1
q)key`a           /now you see it
`a
q)\d .foo
q.foo)key`a       /now you don't
q.foo)a:1 2!3 4
q.foo)key`a       /this one has keys
1 2
q.foo)key`.foo.a  /fully qualified name
1 2
q.foo)key`..a     /fully qualified name
`..a
q.foo)\d .
q)key`a
`a
q)key`.foo.a
1 2
q)key`..a
`..a

Target of a foreign key

Where x is a foreign-key column, returns the name of the foreign-key table.

q)f:([f:1 2 3]v:`a`b`c)
q)x:`f$3 2
q)key x
`f

Type of a vector

Where x is a vector, returns the name of its type as a symbol.

q)key each ("abc";101b;1 2 3h;1 2 3i;1 2 3;1 2 3f)
`char`boolean`short`int`long`float
q)key 0#5
`long

Enumerator of a list

Where x is an enumerated list, returns the name of the enumerating list.

q)ids:`a`b`c
q)x:`ids$`a`c
q)key x
`ids

til

Where x is a non-negative integer atom, returns the same result as til.

q)key 10
0 1 2 3 4 5 6 7 8 9

Metadata