Dictionary 如何在TCL中声明字典中的字典?

Dictionary 如何在TCL中声明字典中的字典?,dictionary,tcl,Dictionary,Tcl,我试图在TCL中声明dictionary of dictionary of dictionary of dictionary of dictionary,但我无法进入第三级创建密钥 这就是我为得到一本字典所做的 set my_dict [dict create] dict append my_dict "key1" dict with my_dict {

我试图在TCL中声明dictionary of dictionary of dictionary of dictionary of dictionary,但我无法进入第三级创建密钥

这就是我为得到一本字典所做的

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"                                                                                                                                                                                                                                                               
}
key1 {key2 key3}
但它返回以下内容:

dict with my_dict {                                                                                                                                                                                                                                                         
    dict append "key1" "key2" "key3"                                                                                                                                                                                                                                                               
}
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 {}}}}

好吧,如果我按照你想做的去做;我会:

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相比,后者的优势在于,后者扩展了整个字典,您无法真正控制变量的变化
dict update
有几种选择方式(例如,您可以使用名称与键不匹配的变量,并且您只能扩展您关心的键)。

初始创建可以通过以下方式完成:

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相比,后者的优势在于,后者扩展了整个字典,您无法真正控制变量的变化
dict update
有几种选择方式(例如,您可以使用名称与键不匹配的变量,并且您只能扩展您关心的键)。

或者干脆
dict set my_dict key1 key2 key3{}
。哦,是的,那就是too或者干脆
dict set my_dict key1 key2 key3{/code>。哦,是的,你是不是认为dict append会给字典添加一个键?事实并非如此。它将字符串连接到字典键的值。这解释了您的“不正确”输出:您正在将字符串“key3”附加到
dict get$my_dict key1 key2
(以前是空字符串)的值上,您是否认为
dict append
会将键添加到字典中?事实并非如此。它将字符串连接到字典键的值。这解释了您的“不正确”输出:您将字符串“key3”附加到
dict get$my_dict key1 key2
的值(之前是空字符串)