Version 3 of dict lappend

Updated 2015-12-15 10:10:58 by oehhar
dict lappend dictionaryVariable key ?value ...?

This appends the given items to the list value that the given key maps to in the dictionary value contained in the given variable, writing the resulting dictionary value back to that variable. Non-existent keys are treated as if they map to an empty list, and it is legal for there to be no items to append to the list. It is an error for the value that the key maps to a value not be representable as a list.

dict lappend for deper nested keys

2015-12-15 HaO: I would love to have a native command which does an lappend to a subdict. E.g.:

% set d {key1 {key2 value1}}
% dictlappendsub d key1 key2 value2
key1 {key2 {value1 value2}}
% dictlappendsub d key1 key3 value3
key1 {key2 {value1 value2} key3 value3}

This might be acheved by:

# dictlappendsub dict key1 ... keyn value
proc dictlappendsub {dictName args} {
        upvar 1 $dictName d
        set keys [lrange $args 0 end-1]
        if {[dict exists $d {*}$keys]} {
                dict set d {*}$keys [linsert [dict get $d {*}$keys] end [lindex $args end]]
        } else {
                dict set d {*}$keys [lrange $args end end]
        }
}