tcl:如何使用变量的值创建新变量

发布于 2024-08-08 05:54:18 字数 272 浏览 2 评论 0原文

这是我正在尝试做的一个例子。

set t SNS
set ${t}_top  [commands that return value]

想要获取存储在 ${t}_top 的信息

puts “${t}_top”
 SNS_top  (really want the data stored there?)

以为是: ${{$t}_top} ,也许那是 perl 但 {} 内的 {} 不起作用。

here is an example of what I'm trying to do.

set t SNS
set ${t}_top  [commands that return value]

Want to get the info stored at ${t}_top

puts “${t}_top”
 SNS_top  (really want the data stored there?)

Thought it was : ${{$t}_top} , maybe that was perl but {} inside the {} do not work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦幻的味道 2024-08-15 05:54:18

Tcl 真正有趣的事情之一是您可以动态创建变量名称,就像您在发布的问题中所做的那样。然而,这使得编写起来很棘手,并且使代码变得比必要的更难理解。

与其试图弄清楚如何执行与 ${{$t}_top} 相同的操作,可以说最好完全避免这个问题。您可以通过使用关联数组来做到这一点。

例如,不要这样做:

set t SNS
set ${t}_top  [commands that return value]
...
puts [set ${t}_top]

这样做:

set t SNS
set top($t) [commands that return value]
...
puts $top($t)

大多数人都认为后一个示例更具可读性。

One of the really interesting things about Tcl is that you can create variable names dynamically, as you are doing in the question you posted. However, this makes it tricky to write and makes your code harder than necessary to understand.

Instead of trying to figure out how to do the equivalent of ${{$t}_top}, it's arguably better to avoid the problem altogether. You can do that by using an associative array.

For example, instead of this:

set t SNS
set ${t}_top  [commands that return value]
...
puts [set ${t}_top]

Do this:

set t SNS
set top($t) [commands that return value]
...
puts $top($t)

Most people agree that the latter example is much more readable.

怎樣才叫好 2024-08-15 05:54:18

尝试

puts [set ${t}_top]

try

puts [set ${t}_top]
¢好甜 2024-08-15 05:54:18

Tcl 中的每一行代码通常只运行一次替换阶段(其中变量、命令等被替换)。因此,类似的事情

set var1 1
set var2 var1
set var3 $var2

不会以 var3 等于 1 结束,因为替换器将用“名为 '$var2' 的变量的值(字面意思)”替换“$$var2”并停止。

你需要它要么以另一种方式处理事情,要么强制进行另一轮替代。另一种方法通常是避免需要第二轮替换(如 Jackson 所示):

set var3 [set $var2]

这里,在替换期间 $var2 被“var1”替换...然后 [set var1] 返回 1...然后 var3被设置为值“1”......并且你很好。

Each line of code in Tcl is run through the substitution phase (in which variables, commands, etc are substituted) only once... generally. As such, something like

set var1 1
set var2 var1
set var3 $var2

won't wind up with var3 equaling 1, since the substitutor will replace "$$var2" with "the value of the variable named '$var2' (literally)" and stop.

What you need it to either go about things another way or to force another round of substitution. The other way is generally to avoid needing a second round of substitution (as shown by Jackson):

set var3 [set $var2]

Here, the $var2 is replaced, during substitution, by "var1"... then [set var1] returns 1... then var3 gets set to the value of "1"... and you're good.

2024-08-15 05:54:18

该语法

puts [expr ${t}_top]

也有效,并避免使用“设置”操作,因此语法错误不应覆盖您的数据。

The syntax

puts [expr ${t}_top]

works as well, and avoids using the 'set' operation so a syntax error shouldn't overwrite your data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文