Skip to content

? Enum Extend

Extend an enumeration

x?y    ?[x;y]

Where

  • y is a list
  • x is a handle to a:

Variable

fills in any missing items in x, then returns y as an enumeration of it. (Unlike Enumerate.)

q)foo:`a`b
q)`foo?`a`b`c`b`a`b`c`c`c`c`c`c`c
`foo$`a`b`c`b`a`b`c`c`c`c`c`c`c
q)foo
`a`b`c

Note that ? preserves the attribute/s of the right-argument but $ does not.

q)`foo?`g#y
`g#`foo$`g#`a`b`c`b`a`b`c`c`c`c`c`c`c
q)`foo$`g#y
`foo$`a`b`c`b`a`b`c`c`c`c`c`c`c

Filepath

fills in any missing items in file x, loads it into the session as a variable of the same name, and returns y as an enumeration of it.

q)bar:`c`d  /about to be overwritten
q)`:bar?`a`b`c`b`a`b`c`c`c`c`c`c`c
`bar$`a`b`c`b`a`b`c`c`c`c`c`c`c
q)\ls -l bar
"-rw-r--r--  1 sjt  staff  16  3 Mar 12:53 bar"
q)bar
`a`b`c

In detail:

`:enumname ?`a`b`c

executes the following steps:

  1. opens the file enumname and locks it (see note)
  2. reads contents of the file enumname, interning each symbol, and binds the resulting symbol vector to enumname
  3. enumerates according to `enumname ?`a`b`c
  4. appends any new symbols to the file `:enumname
  5. closes file enumname, which automatically unlocks it

Locking the file

The file is locked at a process level for writing during .Q.en only. Avoid reading from any file which may be being written to.

The system call used is https://linux.die.net/man/3/lockf.

One can verify that the file system supports the write lock by stracing the following q script locktest.q on the filesystem which you are sharing between those machines:

`:dummysym?`a`b
\\
$ strace q locktest.q 2>&1 | grep F_SETLKW
fcntl(1024, F_SETLKW, {type=F_WRLCK, whence=SEEK_CUR, start=0, len=0}) = 0

If that return value is not 0, then the lock failed and may not be supported by the chosen filesystem. Kdb+ does not report an error if that lock call fails.

Enum Extend is a uniform function.


Enumerate, Enumeration, .Q.en (enumerate varchar cols), ? query overloads
Enumerations, File system
Enumerating varchar columns in a table
Q for Mortals ยง7.5 Enumerations