Reputation: 123
I have difficulty accessing values in nested dictionary by using list of keys.
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
While eval works in this instance - There must be better way to do this directly.
Any idea how to access nested dictionary values by key list in variable?
Upvotes: 1
Views: 3125
Reputation: 5733
You need to expand the list (or string) into separate words.
dict
does not take a list
as an argument.
dict get $testDict {*}$keyLst
References: dict ; argument expansion
Upvotes: 3