Dictionary TCL:更新dict内部;用“口述”;

Dictionary TCL:更新dict内部;用“口述”;,dictionary,tcl,Dictionary,Tcl,我想用dict更新字典,但它似乎没有更新任何值。例如: set test1 [dict create] dict set test1 INST1 ports PORT1 type Q dict set test1 INST1 ports PORT1 reset X dict set test1 INST1 ports PORT2 type Qbar dict set test1 INST1 ports PORT2 reset X dict for {id data} $test1 {

我想用dict更新字典,但它似乎没有更新任何值。例如:

set test1 [dict create]
dict set test1 INST1 ports PORT1 type Q
dict set test1 INST1 ports PORT1 reset X
dict set test1 INST1 ports PORT2 type Qbar
dict set test1 INST1 ports PORT2 reset X

dict for {id data} $test1 {
    set resetVal 0
    dict with data {
        dict for {portName portInfo} $ports {        
            dict with portInfo {
                if {$type == "Q"} {
                    set reset $resetVal
                } elseif {$type == "Qbar"} {
                    set reset [expr $resetVal ^ 1]
                }
           }
       }
    }
}

puts "PORT1=[dict get $test1 INST1 ports PORT1 reset]"
puts "PORT2=[dict get $test1 INST1 ports PORT2 reset]"
它打印:

PORT1=X
PORT2=X

如何使用dict更新“reset”的值?

我不确定您是否可以这样嵌套它们。试一试

set resetVal 0
foreach port {PORT1 PORT2} {
    dict with test1 INST1 ports $port {
        if {$type eq "Q"} {
            set reset $resetVal
        } elseif {$type eq "Qbar"} {
            set reset [expr {$resetVal ^ 1}]
        }
    }
}

文档:,

您可以嵌套它们,但效果可能不是您所期望的
dict with
不使用变量链接(它通过将内容复制回身体末端来工作),因此需要小心。