Tcl Source Code

View Ticket
Login
Ticket UUID: 47fd91176dd16609753b1a00ab3c46c3cc411765
Title: Appending to a seven element dictonary causes issues
Type: Bug Version: 9.x
Submitter: mookie Created on: 2025-01-15 19:37:09
Subsystem: 15. Dict Object Assigned To: nobody
Priority: 5 Medium Severity: Important
Status: Closed Last Modified: 2025-01-16 06:34:29
Resolution: Wont Fix Closed By: oehhar
    Closed on: 2025-01-16 06:34:29
Description:
I noticed this within NaviServer, but noticed it's also effecting TCL9, and prior.

I'm not sure if this is by design, limited or programmed to, or that if you shouldn't be able to. 
However I've started heavily using dictionaries within as a in-house data-store and taken it out of context of the standard dictionary key-value design.  

On count, I am currently creating an dictionary of seven elements. 

  % dict set MyDictonary store set block key field 1 "hello"
store {set {block {key {field {1 hello}}}}}
  % dict set MyDictonary store set block key field 2 "world"
store {set {block {key {field {1 hello 2 hello}}}}}

However, if I try to append another entry to this long dict, it scrambles itself.

  % dict append MyDictonary store set block key field 3 "hello world"
store {set {block {key {field {1 hello 2 world}}}}setblockkeyfield3hello world}

I have a klunky workaround as of retrieving the result, and then appending the new data which then setting that back to the dictionary but it doesn't feel right. 

Am I suppose to be able to append, is this by design? If so, should it allow you to create a seven element dictionary when you cannot append?

Many Thanks.
User Comments: oehhar added on 2025-01-16 06:34:29:

Hi Mookie,

great.

Yes, I would also appreciate more commands working on subkeys, e.g. "dict append" and "dict lappend" for subkeys. Unfortunately, they don't exist and the current command syntax does not allow to implement this easily.

In my daily work, I enjoy the command "pdict" from here: https://wiki.tcl-lang.org/page/pdict%3A+Pretty+print+a+dict

Enjoy, Harald


mookie2 added on 2025-01-15 22:26:36:
Many Thanks for your reply Harald, that now makes sense.
I actually meant, I want to append to an existing key within. I explained it wrongly. But does the same but your answer explains it.


    dict append MyDictonary store set block key field 2 "NewValue"

which I was assuming would then apply "NewValue" to the field 2 key

Result being: store {set {block {key {field {1 hello 2 world NewValue}}}}}

oehhar added on 2025-01-15 19:56:33:

Hi Mookie,

thanks for the question.

"dict append" appends all arguments to the text of the value of the given key.

To use it the way intended:

set d [dict create d1 v1]
dict append d d1 v2 v3
dict get $d d1 -> v1v2v3

So, in your case:

dict append MyDictonary store set block key field 3 "hello world"
is equivalent to
dict append MyDictonary store "setblockkeyfield3hello world"

I think, in your example, you may just use "dict set".

dict set MyDictonary store set block key field 3 "hello world"

If you really want string append, there is no buildin support for nested dictionaries.

So, if you want to append to a string in a nested dictionary, you can only:

dict set MyDictionary store set block key field 3\
        [string cat [dict get $MyDictionary store set block key field 3]\
            "hello world"]

Take care, Harald