我有一个名为 test 的字典,我希望通过向其附加新项目来迭代更新相同的键。所以最终它看起来像这样
目标
test {
1 {flps {o1 o2 o3 ...}}
}
执行此操作的标准方法是什么?我现在有以下代码:
set test [dict create] ;
dict set test 1 "flps" "o1" ; #### first value o1 added
set new "[dict get $test 1 regs] o2" ; ##temp variable that append the old + new value
dict set test 1 regs $new ; ### does dict set overwrite ?
最佳答案
What is the standard way to do this ?
没有用于此目的的内置命令,一种方法是使用dict update
:
% set test [dict create]
% dict update test 1 1 { dict lappend 1 flps "o1" }
flps o1
% set test
1 {flps o1}
% dict update test 1 1 { dict lappend 1 flps "o2" }
flps {o1 o2}
% set test
1 {flps {o1 o2}}
但是,当嵌套级别更多或什至未知的嵌套深度时,这很容易变得不方便。
关于dictionary - TCL 多级字典 - 如何通过附加到相同的键来更新它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60511490/