If we have this dictionary:
set alpha {alice {items {}} bob {items {}} claudia {items {}} derek {items {}}}
And want to add "fork" and "peanut" to Alice's items, this code won't work:
dict lappend alpha alice items fork peanut
dict get $alpha alice
# => items {} items fork peanut
Because it would be impossible for the command to know where the key tokens end and the values to be list-appended start, the command is limited to one key token.
The correct way to append to the inner dictionary is this:
dict with alpha alice {
lappend items fork peanut
}
dict get $alpha alice
# => items {fork peanut}
This works because the dict with
command lets us traverse nested dictionaries, as many levels as the number of key tokens we provide. It then creates variables with the same names as the keys on that level (only one here: items
). The variables are initialized to the value of the corresponding item in the dictionary. If we change the value, that changed value is used to update the value of the dictionary item when the script ends.
(Note that the variables continue to exist when the command has ended.)