From Newsgroup: comp.lang.tcl
greg <
gregor.ebbing@gmx.de> wrote:
I don't understand why the error occurs in my script, or where exactly.
missing value to go with key
Why does an error occur in Scenario 4 when I try to use dict set on a variable initially defined as a list with the same keys and values I
want to add?
#4.
set dvar1 [list k3 v3 k4 v4]
Creates a list of four elements, which when shimmered to a dict will
produce a dict with two keys (k3, k4) each containing one value (v3,
v4) respectively.
dict set dvar1 k3 v3 k4 v4
Attempts to set the chain of keys k3->v3->k4 to the value v4. See the
'dict set' subcommand in the dict manpage, the above is not two keys
with two values but a three level nested dict.
When [dict] looks at dvar1, it finds a k3 key at the top level, it then
looks at the value of k3, finds only "v3" (not a dict) and errors out
because "v3" is not a key.
Why does no error occur in Scenario 5 when the initial list contains different keys and values?
#5.
set dvar1 [list k5 v5 k6 v6]
Creates a list containing four elements, which when shimmered to a
dict, is a dict with two keys (k5, k6) with one value each (v5, v6).
dict set dvar1 k3 v3 k4 v4
Attempts to set the chain of nested keys k3->v3->k4 to the value v4.
When [dict] looks at dvar1, it finds no key "k3", so it creates a new
key "k3", then inside "k3" it creates a new nested dict with a key of
"v3", then inside "v3" it creates a new nested dict with a key of "k4"
and a value of "v4". No error because everything created was all new.
Your test key/value names imply that you think that this:
dict set d1 k1 v1 k2 v2
produces this:
{k1 v1} {k2 v2}
When in fact it produces this:
{k1 {v1 {k2 v2}}}
Those two are two very different dicts. One is a flat dict with two
keys, the other is a three level deep hierarchical dict.
--- Synchronet 3.20a-Linux NewsLink 1.114