问题描述
限时送ChatGPT账号..这是我正在尝试做的一个例子.
here is an example of what I'm trying to do.
set t SNS
set ${t}_top [commands that return value]
想要获取存储在 ${t}_top
Want to get the info stored at ${t}_top
puts "${t}_top"
SNS_top (really want the data stored there?)
认为是: ${{$t}_top} ,也许是 perl 但 {} 中的 {} 不起作用.
Thought it was : ${{$t}_top} , maybe that was perl but {} inside the {} do not work.
推荐答案
关于 Tcl 真正有趣的事情之一是您可以动态创建变量名称,就像您在发布的问题中所做的那样.但是,这使得编写起来很棘手,并且使您的代码更难理解.
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.
与其试图弄清楚如何做相当于 ${{$t}_top} 的事情,可以说完全避免这个问题会更好.您可以通过使用关联数组来做到这一点.
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.
例如,而不是这个:
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)
大多数人都同意后一个例子更具可读性.
Most people agree that the latter example is much more readable.
这篇关于tcl:如何使用变量的值来创建新变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论