Why can’t I index some elements of a dictionary?
Short answer: Check that the keys of your dictionary are uniformly atoms or lists, not both.
To experiment, let’s construct two dictionaries, one with atomic keys and one with list keys.
q)dict1: `keya`keyb`keyc ! 10 20 30
q)dict1 // dictionary with atomic keys
keya| 10
keyb| 20
keyc| 30
q)dict2: (`keya1`keya2; `keyb1`keyb2; `keyc1`keyc2) ! 1 2 3
q)dict2 // dictionary with list keys
keya1 keya2| 1
keyb1 keyb2| 2
keyc1 keyc2| 3
q)dict1[`keya] // index by atom key
10
q)dict2[`keya1`keya2] // index by list key
1
q)
Note how dict1 contains a keys made of exclusively of atoms, and dict2 contains keys that are lists.
Now let’s consider a dictionary whose keys are both atoms and lists:
q)dict3: (`keya1; `keyb1`keyb2; `keyc1`keyc2) ! 1 2 3
q)key dict3
`keya1 // The first key is an atomic symbol,
`keyb1`keyb2 // while the rest of the keys are
`keyc1`keyc2 // lists of symbols.
q)dict3[`keya1] // The first key indexes fine,
1
q)dict3[`keyb1`keyb2] // but the remaining keys are broken;
0N 0N
q)dict3[`keyc1`keyc2] // they are treated as multiple keys.
0N 0N
q)
Even though the keys are present they cannot be found by a dictionary lookup. Reverse lookup does work:
q)dict3
`keya1 | 1 // ok
`keyb1`keyb2| 2 // broken
`keyc1`keyc2| 3 // broken
q)dict3 ? 2
`keyb1`keyb2
q)dict3 ? 3
`keyc1`keyc2
q)
Analogous behavior can be observed when searching a list that contains both atoms and lists:
q)list: key dict3
q)list ? `keya1
0q)list ? `keyb1`keyb2
3 3
q)
Always remember that a dictionary lookup is essentially the following:
q)lookup: {[dict; thekey] value[dict] @ key[dict] ? thekey}
q)
Sometimes types in q behave differently depending on the data contained within. Take a look at this related faq to read more.