Michael Jurgens
Michael Jurgens

Reputation: 39

TCL use a variable to generate a varaible and use a variable for file open/close

As an easy example I just want to loop thorugh opening/closing files and use a variable to create another variable. In PERL this is pretty easy but I cnat figure it out in TCL

set gsrs ""
lappend gsrs "sir"
lappend gsrs "dir"

foreach gsr $gsrs {
  set file "sdrv/icc/instance_toggle_overwrite.$gsr.txt"
  puts "*** I : Generating $file"

  set tempGSR gsr
  puts "$$tempGSR" # would like output to be value of $gsr
  set $gsr [open $file "w"] # normally you would not use a variable here for filename setting
  close $$gsr
}

Upvotes: 0

Views: 63

Answers (1)

Donal Fellows
Donal Fellows

Reputation: 137567

Double-dereferencing is usually not recommended, as it leads to complex code that is quite hard to maintain. However, if you insist on doing it then use set with one argument to do it:

puts [set $tempGSR]

Usually, thinking about using this sort of thing is a sign that either upvar (possibly upvar 0) or an array should be used instead.

Upvotes: 2

Related Questions