cancel
Showing results for 
Search instead for 
Did you mean: 

diff between ! and $ for enums

analyst_tech_jo
New Contributor
I dont understand the difference between ! and $ for enums . Can someone explain ? Wiki is too terse 😞
2 REPLIES 2

AquaQMatt
New Contributor
Hi,

Can you post an example code snippet of what you're not understanding?

Kind regards,
Matt

Jonathon_McMurr
New Contributor
$ is used to create an enumerated list e.g.

q)l:100?`a`b`c`d
q)l
`a`a`c`b`c`b`c`d`c`b`a..
q)x:distinct l
q)`x$l
`x$`a`a`c`b`c`b`c`d`c`..

On the left, there is a variable name (as a symbol) containing all the unique elements of the list to be enumerated. The result is enumerated (as indicated by the `x$ at the start - if you were to store this result, and then change the values in x, the values in the stored enumerated vector would change).

! is used to reconstruct a list from enumerated indices and a list of the unique symbols e.g. (using variable x from above)

q)`x!1 2 3 2 1 3
`x$`c`b`d`b`c`d

Another operator worth noting here is ?. This allows you to enumerate a list against a variable without first assigning all unique elements to the variable e.g.

q)y:() / y is an empty variable
q)`y?l
`y$`a`a`c`b`c`b`c`d`c`..
q)y
`a`c`b`d

As with $, the left operand is a variable to enumerate against, however in this case it does not need to already contain the elements from the list. It can also be updated if used to enumerate another list with additional elements:

q)t:25?`c`d`e`f
q)`y?t
`y$`f`e`f`c`c`d`e`d`e`..
q)y
`a`c`b`d`f`e

Hope that helps
Jonathon