Tcl 整齐地修改内部词典的成员
我正在寻找一种将字符串附加到内部字典(Tcl8.5)的现有成员的方法Tcl 整齐地修改内部词典的成员,tcl,Tcl,我正在寻找一种将字符串附加到内部字典(Tcl8.5)的现有成员的方法 使用dict append是不好的-只启用1级递归 我想出了dict set和dict get的组合,但它非常难看 最后我想,dict with会有帮助,但如果其中一个键与字典的名称相同,那将是灾难性的 上述代码输出: meyaO1hihi missing value to go with key while executing "dict with cfg animals mammal {
- 使用
是不好的-只启用1级递归李>dict append
- 我想出了
和dict set
的组合,但它非常难看李>dict get
- 最后我想,dict with会有帮助,但如果其中一个键与字典的名称相同,那将是灾难性的
meyaO1hihi
missing value to go with key
while executing
"dict with cfg animals mammal {
append cat1 "hihi"
}"
(procedure "dict_append" line 3)
invoked from within
"dict_append cfg"
(procedure "foo" line 9)
invoked from within
"foo $cfg"
(file "basics.tcl" line 20)
Structured key:
animals {mammal {cat1 meyaO1 cat2 meyaO2 cfg bar}}
Formatted key (NOT structured):
{animals mammal cat1} meyaO1 {animals mammal cat2} meyaO2 {animals mammal cfg} bar
你知道一种既干净又安全的方法吗
编辑#12014年2月27日09:15 UTC:
回复@Donal Fellows回答: “你不能做……特殊格式”是什么意思?
我想我可以选择密钥是格式化的还是结构化的。。。下面是一个例子:
puts {Structured key:}
dict set cfg animals mammal cat1 meyaO1
dict set cfg animals mammal cat2 meyaO2
dict set cfg animals mammal cfg bar
puts $cfg
unset cfg
puts {Formatted key (NOT structured):}
dict set cfg "animals mammal cat1" meyaO1
dict set cfg "animals mammal cat2" meyaO2
dict set cfg "animals mammal cfg" bar
puts $cfg
上述代码输出:
meyaO1hihi
missing value to go with key
while executing
"dict with cfg animals mammal {
append cat1 "hihi"
}"
(procedure "dict_append" line 3)
invoked from within
"dict_append cfg"
(procedure "foo" line 9)
invoked from within
"foo $cfg"
(file "basics.tcl" line 20)
Structured key:
animals {mammal {cat1 meyaO1 cat2 meyaO2 cfg bar}}
Formatted key (NOT structured):
{animals mammal cat1} meyaO1 {animals mammal cat2} meyaO2 {animals mammal cfg} bar
那很深。您最好使用嵌套的
dict更新
# Map the value for the 'animals' key as the 'outer' var
dict update cfg animals outer {
# Map the value for the 'mammal' key as the 'inner' var
dict update outer mammal inner {
dict lappend inner cat1 "hihi"
}
}
您不能像这样做结构化键,因为字典设计为允许任意字符串作为键(而结构化键不能与之配合使用,因为无法判断a b c
,甚至a.b.c
,是结构化键还是特定格式的键)
dict with
命令适用于键更为人熟知的情况。@Dor:字典允许您将任何值用作键。如果您想使用列表并将其解释为列表,那绝对没问题。在使用这类词典时,您可能不想将dict与
一起使用。(当我说任何值作为一个键时,我是说它。二进制blob?当然。你可能想避免使用真正大的键;它们可以工作,但可能会减慢速度。)