如何在 TCL 中创建并迭代哈希值的哈希值?

发布于 2024-08-03 05:25:16 字数 771 浏览 7 评论 0原文

如何在 TCL 中创建并迭代哈希值的哈希值?

如果我有这样的数据:

foo = {
    a => {
        aa => { aa1 aa2 aa3 }
        ab => { ab1 ab2 ab3 }
        ac => { ac1 ac2 ac3 }
    }
    b => {
        ba => { ba1 ba2 ba3 }
        bb => { bb1 bb2 bb3 }
        bc => { bc1 bc2 bc3 }
    }
    c => {
        ca => { ca1 ca2 ca3 }
        cb => { cb1 cb2 cb3 }
        cc => { cc1 cc2 cc3 }
    }
}

如何通过一次插入一个叶节点数据项来创建这样的哈希。像这样:

lappend foo(a)(ab) "ab1"

那么如何迭代所有数据元素?喜欢:

foreach key in foo {
    foreach sub_key in foo($key) {
        foreach elem in foo($key)($sub_key) {
            puts "foo\($key\)\($sub_key\) is $elem"
        }
    }
}

编辑: 不幸的是,我无法访问较新的“dict”结构。

How do I create and iterate through a hash of hashes in TCL?

If I have data like:

foo = {
    a => {
        aa => { aa1 aa2 aa3 }
        ab => { ab1 ab2 ab3 }
        ac => { ac1 ac2 ac3 }
    }
    b => {
        ba => { ba1 ba2 ba3 }
        bb => { bb1 bb2 bb3 }
        bc => { bc1 bc2 bc3 }
    }
    c => {
        ca => { ca1 ca2 ca3 }
        cb => { cb1 cb2 cb3 }
        cc => { cc1 cc2 cc3 }
    }
}

How do I create such a hash by inserting one leaf-node data item at a time. Something like:

lappend foo(a)(ab) "ab1"

Then how do I iterate over all data elements? like:

foreach key in foo {
    foreach sub_key in foo($key) {
        foreach elem in foo($key)($sub_key) {
            puts "foo\($key\)\($sub_key\) is $elem"
        }
    }
}

Edit :
Unfortunately, I do not have access to the newer 'dict' construct.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

等风来 2024-08-10 05:25:16

假设您使用的是 Tcl 8.5+,则可以使用字典:

定义字典很简单:

set foo {
    a {
        aa { aa1 aa2 aa3 }
        ab { ab1 ab2 ab3 }
        ac { ac1 ac2 ac3 }
    }
    b {
        ba { ba1 ba2 ba3 }
        bb { bb1 bb2 bb3 }
        bc { bc1 bc2 bc3 }
    }
    c {
        ca { ca1 ca2 ca3 }
        cb { cb1 cb2 cb3 }
        cc { cc1 cc2 cc3 }
    }
}

或以编程方式定义它:

set foo [dict create]
foreach first {a b c} {
    dict update foo $first subdict {
        foreach second {a b c} {
            foreach third {1 2 3} {
                dict lappend subdict "$first$second" "$first$second$third"
            }
        }
    }
}

并输出它:

dict for {key1 subdict} $foo {
    dict for {key2 list} $subdict {
        foreach elem $list {
            puts "$key1\t$key2\t$elem"
        }
    }
}

编辑:移动数组解决方案(非字典)一个单独的答案。

Assuming you're using Tcl 8.5+, dictionaries are the way to go:

Define the dictionary is simply done:

set foo {
    a {
        aa { aa1 aa2 aa3 }
        ab { ab1 ab2 ab3 }
        ac { ac1 ac2 ac3 }
    }
    b {
        ba { ba1 ba2 ba3 }
        bb { bb1 bb2 bb3 }
        bc { bc1 bc2 bc3 }
    }
    c {
        ca { ca1 ca2 ca3 }
        cb { cb1 cb2 cb3 }
        cc { cc1 cc2 cc3 }
    }
}

Or define it programmatically:

set foo [dict create]
foreach first {a b c} {
    dict update foo $first subdict {
        foreach second {a b c} {
            foreach third {1 2 3} {
                dict lappend subdict "$first$second" "$first$second$third"
            }
        }
    }
}

And output it:

dict for {key1 subdict} $foo {
    dict for {key2 list} $subdict {
        foreach elem $list {
            puts "$key1\t$key2\t$elem"
        }
    }
}

edit: moved the array solution (non-dict) to a separate answer.

如痴如狂 2024-08-10 05:25:16

如果您不使用 Tcl 8.5,则可以使用数组。请注意,数组是一维的,但键是任意字符串,可用于伪造多维性:

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

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

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

如果给定键(例如'b' 和 'bc') 你可以这样得到值:

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

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"
    }
}

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}
九命猫 2024-08-10 05:25:16

如果您只想在没有 dict 命令的情况下迭代 dict(这只是一个键值对列表),那么您可以简单地使用 foreach 的强大功能:

set foo {
  a {
    aa { aa1 aa2 aa3 }
    ab { ab1 ab2 ab3 }
    ac { ac1 ac2 ac3 }
  }
  b {
    ba { ba1 ba2 ba3 }
    bb { bb1 bb2 bb3 }
    bc { bc1 bc2 bc3 }
  }
  c {
    ca { ca1 ca2 ca3 }
    cb { cb1 cb2 cb3 }
    cc { cc1 cc2 cc3 }
  }
}

foreach {key value} $foo {
  foreach {sub_key sub_value} $value {
    foreach elem $sub_value {
      puts "foo\($key\)\($sub_key\) is $elem"
    }
  }
}

另一方面,如果没有 dict,一次插入一个元素是痛苦的命令:

set foo {}
lappend foo a {}
set a_index [lsearch $foo a]
set a_value_index [expr {$a_index+1}]
set a_value [lindex $foo $a_value_index]
lappend a_value aa {}
lset foo $a_value_index $a_value
# it is now too painful for me to continue :-(

幸运的是,您可以使用 dict 命令的纯 tcl 实现: 向前兼容的 dict

If you just want to iterate through a dict (which is simply a key-value pair list) without the dict command then you can simply use the awesomeness of foreach:

set foo {
  a {
    aa { aa1 aa2 aa3 }
    ab { ab1 ab2 ab3 }
    ac { ac1 ac2 ac3 }
  }
  b {
    ba { ba1 ba2 ba3 }
    bb { bb1 bb2 bb3 }
    bc { bc1 bc2 bc3 }
  }
  c {
    ca { ca1 ca2 ca3 }
    cb { cb1 cb2 cb3 }
    cc { cc1 cc2 cc3 }
  }
}

foreach {key value} $foo {
  foreach {sub_key sub_value} $value {
    foreach elem $sub_value {
      puts "foo\($key\)\($sub_key\) is $elem"
    }
  }
}

on the other hand, inserting elements one at a time is painful without the dict command:

set foo {}
lappend foo a {}
set a_index [lsearch $foo a]
set a_value_index [expr {$a_index+1}]
set a_value [lindex $foo $a_value_index]
lappend a_value aa {}
lset foo $a_value_index $a_value
# it is now too painful for me to continue :-(

fortunately you can use a pure-tcl implementation of the dict command: forward-compatible dict

岁月静好 2024-08-10 05:25:16

如果您没有 Tcl 8.5 字典,请使用键控列表命令来完成工作。您可以通过 google 搜索以下术语之一:keylget、keylset。

package require Tclx

# Create the nested structure
catch {unset foo}
foreach key1 {a b c} {
    foreach key2 {a b c} {
        catch {unset element}
        foreach key3 {1 2 3} {
            lappend element "$key1$key2$key3"
        }
        keylset foo $key1.$key1$key2 $element
    }
}

# Access the nested structure
foreach key1 {a b c} {
    foreach key2 {a b c} {
        set elementList [keylget foo $key1.$key1$key2]
        foreach element $elementList {
            puts "foo\\$key1\\$key1$key2\\$key3 = $element"
        }
    }
}

#
# Access examples
#

# Access a block of data
puts "foo\\a = [keylget foo a]"
# Access a nested block of data
puts "foo\\b\\ba = [keylget foo b.ba]"
# Access an individual element, remember that Tcl's list index is 0 based
puts "foo\\c\\cb\\1 = [lindex [keylget foo c.cb] 0]"

If you don't have the luxury of the Tcl 8.5 dictionary, use the keyed list commands to get the job done. You can google for one of these terms: keylget, keylset.

package require Tclx

# Create the nested structure
catch {unset foo}
foreach key1 {a b c} {
    foreach key2 {a b c} {
        catch {unset element}
        foreach key3 {1 2 3} {
            lappend element "$key1$key2$key3"
        }
        keylset foo $key1.$key1$key2 $element
    }
}

# Access the nested structure
foreach key1 {a b c} {
    foreach key2 {a b c} {
        set elementList [keylget foo $key1.$key1$key2]
        foreach element $elementList {
            puts "foo\\$key1\\$key1$key2\\$key3 = $element"
        }
    }
}

#
# Access examples
#

# Access a block of data
puts "foo\\a = [keylget foo a]"
# Access a nested block of data
puts "foo\\b\\ba = [keylget foo b.ba]"
# Access an individual element, remember that Tcl's list index is 0 based
puts "foo\\c\\cb\\1 = [lindex [keylget foo c.cb] 0]"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文