如何在Tcl中为每个键打印多个值?

时间:2015-06-22 14:55:38

标签: arrays tcl

array set array_in_twos {
    set1 table
    set2 chair
    set1 chair
}

foreach combo [array names array_in_twos] {
    puts "$combo is  $array_in_twos($combo),"
}

输出:

set1 is chair,
set2 is chair,

似乎是第二个'设置1'替换第一个'集1'。 我怎么打印所有?

set1 is table,
set2 is chair,
set1 is chair,

如果使用数组不是最佳解决方案,我会对其他方法持开放态度。感谢。

1 个答案:

答案 0 :(得分:3)

您无法使用数组词典执行此操作;两者都是从键到值的映射。相反,您需要直接将foreach与键值对系统一起使用:

set pairs {
    set1 table
    set2 chair
    set1 chair
}

foreach {key value} $pairs {
    puts "$key is $value"
}

这实际上缩短了代码......