如何将包含更多参数的字典传递到tcl中的proc中?

时间:2012-03-09 04:32:34

标签: tcl

proc test {a b c } {
       puts $a
       puts $b
       puts $c
}
set test_dict [dict create a 2 b 3 c 4 d 5]

现在我想将dict传递给像这样的测试:

test $test_dict

如何使test仅选择dict中的三个元素,并使用相同的参数名称(键)。预期的输出应为:

2
3
4

因为它在字典中选择a b c而不是d。我怎样才能做到这一点?我看到一些代码确实如此,但我无法使其工作。

1 个答案:

答案 0 :(得分:5)

我认为你应该使用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

编辑: 另一种变体是使用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

但是test仍然是一个列表。

相关问题