Skip to content
Tcl Source Code View Ticket
Login

View Ticket

Inconsistency in 'dict set'

Closed / Fixed1037235fffffffffffffffffffffffffffffffffCreated 22 years agoUpdated 22 years ago
Descriptionplain2004-09-29 19:36:48
The goal is to create a nested dictionary (foo) with
the following mappings:

    a -> A
    bc -> b -> B
    bc -> c -> C

One would expect to do it by:

    dict set foo a A
    dict set foo bc b B
    dict set foo bc c C

But this leads to the error (key "bc" not known in
dictionary). Instead it has to be done by:

    dict set foo a A
    dict set foo bc [dict create b B c C]

After the mappings are created, the values can be
changed by:

    dict set foo a AA
    dict set foo bc b BB
    dict set foo bc c CC

The different treatment of mappings at the 'top level'
and at a 'lower level' should either be
removed/corrected or mentioned in the docu.
dkfplain2004-09-30 17:31:49
Logged In: YES 
user_id=79902

Workaround:
Put
   dict set foo bc {}
before you set the components of bc
Problem: only safe when know element does not exist or is empty

Workaround #2:
Put
   dict append foo bc
sometime before you set the components of bc; this will
create the dictionary entry if it does not already exist,
and will otherwise have no effect. (i.e. it is safe and
won't lose data. As it happens, it also won't do loads of
shimmering either, which is even better!)
dkfplain2004-09-30 17:53:30
Logged In: YES 
user_id=79902

Question:
  What do you think the following should do:
     set foo {}
     dict set foo a A
     dict unset foo bc b
     dict set foo bc c C
(I ask because it affects exactly what kind of fix is to be
designed.)
dkfplain2004-09-30 19:18:11
Logged In: YES 
user_id=79902

BTW, the difference is currently between the leaf dictionary
and the branch dictionaries.
muehlebachsplain2004-10-02 02:27:52
Logged In: YES 
user_id=1130415

Thanks for your respons(es). The first workaround looks
quite reasonable to me.

Answers to your question:

    set foo {}
    dict set foo a A
>>> creates a new mapping in 'foo': a->A
    dict unset foo bc b
>>> error! since there is no key 'ab' in 'foo'
    dict set foo bc c C
>>> creates a new mapping in 'foo': bc->c->C

So the resulting dictionary should look like:

    a A bc {c C}
dkfplain2004-10-04 14:53:37
Logged In: YES 
user_id=79902

Thanks for your feedback; that's now the way that
dictionaries behave in the HEAD. :^)
Log in (anonymous is fine) to add a comment.