TCL:在“dict with”中更新dict

时间:2015-06-17 06:45:01

标签: 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 {
    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”的值

1 个答案:

答案 0 :(得分:1)

我不确定你能不能那样筑巢。尝试

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}]
        }
    }
}

文档:dictexprforeachifset