Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2025-01-16
06:34 • Closed ticket [47fd91176d]: Appending to a seven element dictonary causes issues... artifact: 093fb455a7 user: oehhar
2025-01-15
22:26 • Ticket [47fd91176d]: 3 changes artifact: 9e31babf54 user: mookie2
19:56 • Ticket [47fd91176d]: 4 changes artifact: 38a1220335 user: oehhar
19:37 • New ticket [47fd91176d]. artifact: 21564b2970 user: mookie

Appending to a seven element dictonary causes issues

Closed / Wont Fix47fd91176dd16609753b1a00ab3c46c3cc411765Created 2 years agoUpdated 2 years ago
Descriptionplain2025-01-15 19:37:09
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.
oehharwiki2025-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

mookie2plain2025-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}}}}}
oehharwiki2025-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

Log in (anonymous is fine) to add a comment.