Tcl: how to use a variable value to create a new variable

here is an example of what i am trying to do.

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

Want to get the information stored in $ {t} _top

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

Thought it was: $ {{$ t} _top}, maybe it was perl, but {} inside {} doesn't work.

+3
source share
4 answers

One of the really interesting things about Tcl is that you can create variable names dynamically, as you do in the question you posted. However, this makes writing difficult and makes your code more complex than you need to understand.

Instead of trying to figure out how to make the equivalent of $ {{$ t} _top}, it's better to avoid the problem at all. You can do this using an associative array.

For example, instead:

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

Do it:

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

, .

+7

puts [set ${t}_top]
+2

Tcl ( , .. ) ... . , -

set var1 1
set var2 var1
set var3 $$var2

var3, 1, "$$ var2" " " $var2 "()" .

, , . , ( ):

set var3 [set $var2]

$var2 "var1"... [set var1] 1... var3 "1"... .

+2

puts [expr $${t}_top]

"set", .

0

Source: https://habr.com/ru/post/1720894/


All Articles