【问题标题】:how to push multiple values into an array in TCL如何将多个值推送到 TCL 中的数组中
【发布时间】:2022-12-12 05:03:00
【问题描述】:

我正在尝试将值附加到数组,如下所示,但它没有附加到值列表

while {[gets $fp line] != -1} {

    if { [regexp {Path Group: (\w+)} $line all group]} {
        set hash($group) {}
 
    } elseif {[regexp {\(VIOLATED\)\s+(-[0-9]*.[0-9]*)} $line all slack]} {
        puts "slack $slack\n"
         lappend hash($group) $slack     
    }

 }

parray hash

数组哈希的输出只是迭代位的最后一个值而不是列表

期待

名称 { X Y Z} 年龄 {3 4 5:}

【问题讨论】:

  • 样本输入会很好。

标签: arrays dictionary tcl


【解决方案1】:
set group {}
while {[gets $fp line] != -1} {
  if { [regexp {Path Group: (w+)} $line all group]} {
  } elseif {[regexp {(VIOLATED)s+(-[0-9]*.[0-9]*)} $line all slack]} {
    puts "slack $slack
"
    lappend hash($group) $slack     
  }
}
parray hash
  • 无需在每次匹配时设置hash($group),因为这将重置hash($group)的内容。
  • lappend hash($group)中,如果变量不存在,会自动创建。

【讨论】:

    猜你喜欢
    • 2013-11-09
    • 1970-01-01
    • 1970-01-01
    • 2015-02-09
    • 1970-01-01
    • 2016-07-07
    • 1970-01-01
    • 2015-11-20
    • 1970-01-01
    相关资源
    最近更新 更多