# Enumeration

For a long list containing few distinct values, an enumeration can reduce storage requirements. The ‘manual’ way to create an enum (for understanding, not recommended):

q)y:abcbabccccccc
q)x:abc
q)show e:"i"$x?y; 0 1 2 1 0 1 2 2 2 2 2 2 2i /these values are what we store instead of y. q)x e /get back the symbols any time from x and e. abcbabccccccc q)x!e / same result as x$y
x$abcbabccccccc  ## $ (enum)¶

Syntax: x $y Where x and y are lists, x~distinct x, and distinct y are all items of x, returns y as an enumeration of x. Using built-in enum: q)show e:x$y;
x$abcbabccccccc  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$obcbobccccccc 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
obcbobccccccc


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:abcbabccccccc q)x:ab q)x$y
'cast

To expand the domain, use ? (enum-extend) instead of $. ## ? (enum-extend)¶ Syntax: x ? y Where y is a list and x is • a symbol naming a variable, fills in any missing items in x, returns y as an enumeration of the variable named in x with filling in missing items in it. (Unlike enum.) q)foo:ab q)foo?abcbabccccccc foo$abcbabccccccc
q)foo
abc


Note that ? retains the attr of the right-argument but $ does not. q)foo?g#y g#foo$g#abcbabccccccc
q)foo$g#y foo$abcbabccccccc

• a filepath symbol and y is a list, 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:cd  /about to be overwritten
q):bar?abcbabccccccc
bar$abcbabccccccc q)\ls -l bar "-rw-r--r-- 1 sjt staff 16 3 Mar 12:53 bar" q)bar abc  ## ! (enumerate)¶ Syntax:x ! y Where x is a symbol atom naming a variable containing a symbol vector, and y is an int vector of indexes into x, returns an enumerated symbol list. q)x:abcd q)x!1 2 3 x$bcd