Search code examples
kdb

Type error while creating dictionary with single value


When I'm creating dictionary with single int/long value then dictionary is created

`ke!1 / Output- `ke!1i

But when I'm creating dictionary with value as single symbol, character or string then it is throwing type error.

`ke!`a    / 'type
`ke!"a"   / 'type
`ke!"abc" / 'type
`ke!enlist(`a) / `type
`ke!(enlist "a") / 'type

Tried various more combinations but of no help.
'Q for Mortals' and 'Reference Card' also not providing any understanding.
Please let me know the reason behind type error and how can it be resolved?


Solution

  • ! is quite overloaded in q. It this case, when the left hand side parameter is a symbol atom, you are actually creating an enumeration.

    q)type `ke!1i
    -21h
    

    You can create your desired dictionary by enlisting both sides.

    q)type(enlist `ke)!enlist 1i
    99h
    

    This page provides a good summary on the different uses of !.