Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I output a list of the values for all variables from the info globals command?

Tags:

tcl

I am trying to write a small bit of code that will list all of the variables and the values they contain in the info globals command. I have tried several iterations of substitution but cant get Tcl to treat the variable name as a variable, and return its value. Below is what I started with:

set fileid [open "c:/tcl variables.txt" w+]
foreach {x}  [lsort [info globals]]  {
    set y $x
    puts $fileid "$x  $y   "
}

I can get

DEG2RAD  DEG2RAD   
PI  PI   
RAD2DEG  RAD2DEG   
.....

or

DEG2RAD  $DEG2RAD   
PI  $PI   
RAD2DEG  $RAD2DEG   
.....

but what I need is

DEG2RAD  0.017453292519943295   
PI  3.1415926535897931   
RAD2DEG  57.295779513082323   
....   
like image 831
user1402185 Avatar asked Dec 16 '22 00:12

user1402185


1 Answers

I think you are looking for the subst command:

set fileid [open "c:/tcl variables.txt" w+] 
foreach {x} [lsort [info globals]] {
  puts $fileid "$x [subst $$x] " 
}

Alternatively, you can take advantage of the fact that set returns the value being set:

set fileid [open "c:/tcl variables.txt" w+] 
foreach {x} [lsort [info globals]] {
  puts $fileid "$x [set $x] " 
}
like image 180
ramanman Avatar answered May 04 '23 17:05

ramanman