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

How do I print more than one value per key in 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,

似乎第二个 'set 1' 取代了第一个 'set 1'。 我如何打印全部?

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

如果使用数组不是最佳解决方案,我愿意接受其他方法。谢谢

你不能用数组字典来做;两者都是从键到值的映射。相反,您需要直接将 foreach 与键值对系统一起使用:

set pairs {
    set1 table
    set2 chair
    set1 chair
}

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

这确实缩短了代码……