【问题标题】:How to pass variable to value in dictionary如何将变量传递给字典中的值
【发布时间】:2021-12-26 18:09:29
【问题描述】:

我是 TCL 字典的新手。我有一本字典,我已经使用变量来表示字典中的值。例如

   set first "Varun"
   set last "Kumar"
   set name [dict create "ID_1" {prename "$first" surname "$last"}]
   set nm  [dict get $name ID_1]
   puts $nm

输出为“prename $first surname $last”。

你能帮我解决这个问题吗

【问题讨论】:

    标签: dictionary tcl


    【解决方案1】:

    我没有时间给出深入的答案,也许其他人可以帮助补充。

    在 Tcl 中,大括号防止所有替换,包括变量替换。

    要创建多级字典,您需要使用list 命令

       set name [dict create "ID_1" [list prename $first surname $last]]
       puts [dict get $name ID_1 surname]     ;# => Kumar
    

    注意,与 shell 编程不同,不需要引用变量。

    您可能想试一试the Tcl tutorial12 rules of Tcl syntax


    正如 Donal 所建议的,零碎构建可以避免所有引用问题。

    dict set name ID_1 prename $first
    dict set name ID_1 surname $last
    

    【讨论】:

    • 或者,使用dict set 逐步构建它。
    猜你喜欢
    • 1970-01-01
    • 2014-09-19
    • 1970-01-01
    • 1970-01-01
    • 2015-02-10
    • 2018-12-23
    • 1970-01-01
    • 1970-01-01
    • 2013-08-03
    相关资源
    最近更新 更多