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

编程入门 行业动态 更新时间:2024-10-28 12:19:42
本文介绍了tcl:如何使用变量的值来创建新变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送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:关键词]

本文发布于:2023-05-01 09:20:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1407273.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   如何使用   tcl

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!