且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 TCL 中创建和迭代散列哈希?

更新时间:2023-08-20 12:26:04

如果您没有使用 Tcl 8.5,那么您可以使用数组.请注意,数组是一维的,但键是可用于伪造多维的任意字符串:

If you're not using Tcl 8.5, then you can use arrays. Note that arrays are one-dimensional, but the key is an arbitrary string that can be used to fake multi-dimensionality:

array set foo {}
foreach first {a b c} {
    foreach second {a b c} {
        foreach third {1 2 3} {
            lappend foo($first,$first$second) "$first$second$third"
        }
    }
}
parray data

并输出——注意:数组键与字典键不同,是无序的:

and output it -- note: array keys, unlike dictionary keys, are unordered:

foreach key [array names foo] {
    foreach elem $foo($key) {
        puts "$key\t$elem"
    }
}

如果您获得键(例如b"和bc"),您可以这样获得值:

If you are given the keys (example 'b' and 'bc') you can get the value thusly:

set key1 b
set key2 bc
foreach elem $foo($key1,$key2) {puts $elem}