如果有列表,则列表项将从其他文件中获取。我想将每个列表项设置为数组,我这样做:
set lista [element1 element2 element3 ...]
foreach item $lista {
array set $item {1 a 2 b 3 c}
}
如果我想添加键值:
set test elment1
array set $test {4 d}
我想知道还有其他更好的结构来设置这些动态数组吗?谢谢!
答案 0 :(得分:0)
使用dict
将为您提供您正在寻找的灵活数据结构:
set lista [list elem1 elem2 elem3]
set initial_map {1 a 2 b 3 c}
set d [dict create]
foreach elem $lista {
dict set d $elem $initial_map
}
puts $d
# => elem1 {1 a 2 b 3 c} elem2 {1 a 2 b 3 c} elem3 {1 a 2 b 3 c}
puts [dict get $d elem2 2]
# => b
# add a new key-value to "elem3"
dict set d elem3 4 d
puts $d
# => elem1 {1 a 2 b 3 c} elem2 {1 a 2 b 3 c} elem3 {1 a 2 b 3 c 4 d}
# manipulate the "sub-dictionaries"
puts [dict keys [dict get $d elem3]]
# => 1 2 3 4