Tcl placing a value of a variable as the name a the variable

后端 未结 2 642
情书的邮戳
情书的邮戳 2021-01-23 00:04

I\'m having some issues with Tcl. I have a variable that has a string in it. Butt now I want this string to be the name of a next variable.

I have found some similar que

相关标签:
2条回答
  • 2021-01-23 00:33

    The correct solution is to use $name as first parameter to set

    set name "foo"
    set $name "bar"
    puts $foo ;# -> bar
    

    But if you try to use $name it will yield foo, not bar (Which is what you do in your code according to the comments. I don't know why you need a variable whose name you don't know, but anyway:

    puts [set $name] ;# -> bar
    

    will give you the correct thing. The same works with objects:

    set topcell [[set $name] topcell]
    

    So I have to ask you: what do you want to do with the dynamic named variable then?

    0 讨论(0)
  • 2021-01-23 00:33

    I'm thinking you want upvar to create an "alias" variable name

    $ tclsh
    % set name Test
    Test
    % upvar 0 cell $name
    % set cell 42
    42
    % puts $Test
    42
    
    0 讨论(0)
提交回复
热议问题