Tcl 将变量的值作为变量的名称 [英] Tcl placing a value of a variable as the name a the variable
问题描述
我在使用 Tcl 时遇到了一些问题.我有一个变量,里面有一个字符串.现在我希望这个字符串是下一个变量的名称.
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 questions on the web, but these are all about placing the value from a variable into another variable. not using it as the name of the variable.
以下是帮助解释的示例代码:
Here is a sample code to help explaining:
foreach key [array names ::openedFiles] {
puts $::openedFiles($key)
set filename [file tail $::openedFiles($key)]
set parts [split $filename .]
set name [lindex $parts 0]
puts $name
$L1 create cell $name
set "value of $name" [ layout create $::openedFiles($key) -dt_expand -log LUN.log]
所以基本上它必须执行以下操作.该数组中有一些路径字符串.我只从路径中获取文件名,而没有文件扩展名.
So bassicly it has to do the following. The array has some path string in it. I get only the name of the file out of the path without the file extension.
然后我想创建一个变量cell",它是$name"的值.所以当文件名是Test"时,$name 的值将是Test",我想像这样做最后一行
Butt then I want to create a variable "cell" that is the value of "$name". So when the filename is "Test", the value of $name will be "Test" and I want to do the last line like this
set Test [ layout create $::openedFiles($key) -dt_expand -log LUN.log]
这样 $name 的值将成为新变量的名称.因此,我可以创建一个变量,其中包含数组中所有值的名称.
So that the value of $name will be the name of the new variable. And so I can creat a variable with the name of all the values in the array.
任何帮助或指示都会很棒!
Any help or pointers would be great!
非常感谢!
推荐答案
正确的解决方法是使用 $name
作为第一个参数来设置
The correct solution is to use $name
as first parameter to set
set name "foo"
set $name "bar"
puts $foo ;# -> bar
但是如果您尝试使用 $name
它将产生 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?
这篇关于Tcl 将变量的值作为变量的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!