proc test {a b c } {
puts $a
puts $b
puts $c
}
set test_dict [dict create a 2 b 3 c 4 d 5]
Run Code Online (Sandbox Code Playgroud)
现在我想将dict传递给测试,如下所示:
test $test_dict
Run Code Online (Sandbox Code Playgroud)
如何test只在dict中选择三个元素,并使用相同的参数名称(键).预期产量应为:
2
3
4
Run Code Online (Sandbox Code Playgroud)
因为它a b c在字典中选择但不是d.我怎样才能做到这一点?我看到一些代码确实如此,但我无法使它工作.
我认为你应该使用dict get:
proc test {test_dic} {
puts [dict get $test_dic a]
puts [dict get $test_dic b]
puts [dict get $test_dic c]
}
set test_dict [dict create a 2 b 3 c 4 d 5]
test $test_dict
Run Code Online (Sandbox Code Playgroud)
编辑:另一种变体是使用dict with:
proc test {test_dic} {
dict with test_dic {
puts $a
puts $b
puts $c
}
}
set test_dict [dict create a 2 b 3 c 4 d 5]
test $test_dict
Run Code Online (Sandbox Code Playgroud)
但test仍然是一个清单.