我很難通過使用鍵列表來訪問嵌套字典中的值。Tcl:如何通過鍵列表從嵌套字典中獲取值
dict set testDict library [dict create NY [dict create section [dict create adult [dict create book cinderella]]]]
library {NY {section {adult {book cinderella}}}}
# I can access the value by:
dict get $testDict library NY section adult book
cinderella
# cannot access the same by list of keys in a variable
set keyLst {library NY section adult book}
library NY section adult book
set keyStr "library NY section adult book"
library NY section adult book
dict get $testDict $keyLst
key "library NY section adult book" not known in dictionary
dict get $testDict $keyStr
key "library NY section adults book" not known in dictionary
# The only not elegant solution I came up is using eval + list
eval dict get \$testDict $keyStr
key "adults" not known in dictionary
eval dict get \$testDict $keyLst
cinderella
雖然eval在這種情況下工作 - 必須有更好的方式來直接做到這一點。
任何想法如何通過變量中的鍵列表訪問嵌套字典值?
如果它只是代碼風格,那麼你可以在[代碼評論](http://codereview.stackexchange.com/)上提問。 –