如何在TCL中声明字典的字典?
How to declare dictionary of dictionary of dictionaries in TCL?
我正在尝试在 TCL 中声明字典的字典,但我无法进入第 3 级来创建密钥。
这就是我得到字典的字典所做的
set my_dict [dict create]
dict append my_dict "key1"
dict with my_dict {
dict append "key1" "key2"
}
puts $my_dict
>> key1 {key2 {}}
这符合预期。现在我需要另一个级别的字典如下:
key1 {key2 {key3 {}}}
如何实现?
我尝试执行以下操作:
dict with my_dict {
dict append "key1" "key2" "key3"
}
但它 returns 以下内容:
key1 {key2 key3}
好吧,如果我按照你想做的去做;我会这样做:
set my_dict [dict create]
dict append my_dict "key1"
dict with my_dict {
dict append "key1" "key2"
dict with key1 {
dict append "key2" "key3"
}
}
puts $my_dict
但我认为最简单的使用dict命令仍然是:
set my_dict [dict create key1 [dict create key2 [dict create key3 ""]]]
如果你不想简单地做:
set my_dict {key1 {key2 {key3 {}}}}
初始创建只需:
dict set my_dict key1 key2 key3 {}
但是附加到那个内部字典有点尴尬。 (并非所有 dict
子命令都直接支持嵌套字典;语法上的歧义太多,无法正常工作。)这是 dict update
特别有用的地方:
dict update my_dict $key1 subdict1 {
dict update subdict1 $key2 subdict2 {
dict append subdict2 $key3 "foo bar"
}
}
与 dict with
相比的优势在于后者扩展了整个字典,并且您无法真正控制更改哪些变量; dict update
在几个方面具有选择性(例如,您可以使用名称与键不匹配的变量,并且您只能扩展您关心的键)。
我正在尝试在 TCL 中声明字典的字典,但我无法进入第 3 级来创建密钥。
这就是我得到字典的字典所做的
set my_dict [dict create]
dict append my_dict "key1"
dict with my_dict {
dict append "key1" "key2"
}
puts $my_dict
>> key1 {key2 {}}
这符合预期。现在我需要另一个级别的字典如下:
key1 {key2 {key3 {}}}
如何实现?
我尝试执行以下操作:
dict with my_dict {
dict append "key1" "key2" "key3"
}
但它 returns 以下内容:
key1 {key2 key3}
好吧,如果我按照你想做的去做;我会这样做:
set my_dict [dict create]
dict append my_dict "key1"
dict with my_dict {
dict append "key1" "key2"
dict with key1 {
dict append "key2" "key3"
}
}
puts $my_dict
但我认为最简单的使用dict命令仍然是:
set my_dict [dict create key1 [dict create key2 [dict create key3 ""]]]
如果你不想简单地做:
set my_dict {key1 {key2 {key3 {}}}}
初始创建只需:
dict set my_dict key1 key2 key3 {}
但是附加到那个内部字典有点尴尬。 (并非所有 dict
子命令都直接支持嵌套字典;语法上的歧义太多,无法正常工作。)这是 dict update
特别有用的地方:
dict update my_dict $key1 subdict1 {
dict update subdict1 $key2 subdict2 {
dict append subdict2 $key3 "foo bar"
}
}
与 dict with
相比的优势在于后者扩展了整个字典,并且您无法真正控制更改哪些变量; dict update
在几个方面具有选择性(例如,您可以使用名称与键不匹配的变量,并且您只能扩展您关心的键)。