$ Enumerate

Syntax: x$y, $[x;y]

Where

  • x and y are lists
  • x~distinct x
  • items of y are all items of x

returns y as an enumeration of x. Using built-in Enumerate:

q)show e:`x$y;
`x$`a`b`c`b`a`b`c`c`c`c`c`c`c

Values are stored as indexes and so need less space.

q)"i"$e
0 1 2 1 0 1 2 2 2 2 2 2 2i

Changing one lookup value (in x) has the same effect as changing those values in the enumeration, while the indexes backing e are unchanged.

q)x[0]:`o
q)e
x$`o`b`c`b`o`b`c`c`c`c`c`c`c
q)"i"$e
0 1 2 1 0 1 2 2 2 2 2 2 2i

To get x and y from e:

q)(key;value)@\:e
`x
`o`b`c`b`o`b`c`c`c`c`c`c`c

Ensure all items of y are in x

When creating an enumeration using $, the domain of the enumeration must be in x, otherwise a cast error will be signalled.

q)y:`a`b`c`b`a`b`c`c`c`c`c`c`c
q)x:`a`b
q)`x$y
'cast

To expand the domain, use ? (Enum Extend) instead of $.

Errors

error cause
cast item/s of y not in x

Enum Extend, Enumeration
Basics: Enumerations
Q for Mortals: §7.5 Enumerations
$ dollar