Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tcl: dictionary inside dictionary

Tags:

dictionary

tcl

I seem to have problem with getting Jim and Bob in the list named chicken. I know this is possible, to have a dictionary inside another dictionary which has a list of values. What am I doing wrong?

puts [dict set farm tools hoe]
puts [dict lappend farm tools pitchfork]
puts [dict set farm animal cow Marry]
puts [dict set farm animal chicken Jim]
puts [dict lappend farm animal chicken Bob]
puts "chicken list: [dict get $farm animal chicken]"
puts "tool list: [dict get $farm tools]"

Output

tools hoe
tools {hoe pitchfork}
tools {hoe pitchfork} animal {cow Marry}
tools {hoe pitchfork} animal {cow Marry chicken Jim}
tools {hoe pitchfork} animal {cow Marry chicken Jim chicken Bob}
chicken list: Bob
tool list: hoe pitchfork

As you can see the tools list worked. but chicken list didn't work. Is there a problem in the way I structure my code? Im using dict lappend or dict get wrong? Anything would help to shed some light on this, Thank you.

like image 567
Darkninja Avatar asked Nov 02 '22 13:11

Darkninja


1 Answers

So I went to tcl chat room and they told me:

As described in the man page, dict lappends both chicken and Bob to the list in dict element animal.

You can't directly append to a list deeper in the dict.

One way would be:

dict with farm {
    dict lappend animal chicken Jim Bob
}

so ya, my bad. :P all credit goes to "schelte" in the tcl chat room.

Extra: the reason for why dict lappend works the way it does comes form Donal Fellows himself in the comments above, thank you Donal

like image 127
Darkninja Avatar answered Nov 28 '22 08:11

Darkninja