【问题标题】:Passing a dictionary value to and fro in TCL在 TCL 中来回传递字典值
【发布时间】:2022-01-20 09:08:53
【问题描述】:

我正在尝试编写一个脚本,它使用 dictionary 值来存储一些数据。然后,我对 procedure 使用相同的字典值进行进一步处理。它看起来像这样:

proc testProc {a} {
   some statements ...
   dict incr a key2 1
}

set dummy [dict create]
dict incr dummy key1 1
testProc $dummy 

puts "[dict get $dummy]"

我希望得到如下输出:

key1 1 key2 1

相反,我得到了:

key1 1

如果我想要我的输出第一个,那我应该怎么做。

谢谢

【问题讨论】:

    标签: dictionary tcl procedure


    【解决方案1】:

    testProc 更新字典的副本。您可以返回修改后的文件并保存:

    proc testProc {a} {
        # some statements ...
        dict incr a key2 1
        return $a
    }
    
    set dummy [dict create]
    dict incr dummy key1 1
    set dummy [testProc $dummy]
    puts [dict get $dummy]
    

    或传递保存字典的变量的名称并使用upvar(类似于dict incr等的工作方式):

    proc testProc {aDict} {
        upvar $aDict a
        # some statements ...
        dict incr a key2 1
    }
    
    set dummy [dict create]
    dict incr dummy key1 1
    testProc dummy
    puts [dict get $dummy]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-30
      • 2012-09-15
      • 1970-01-01
      • 2019-01-03
      • 2021-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多