How can I output a list of the values for all variables from the info globals command?
tcl
Solution
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] "
}
Problem
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 .... ```