Reputation: 43
I am reading set of lines from text file. It's laid out like this
kCode %title My Title
kCode %content Hello World
pCode %picture MyVar
I have MyVar defined:
set MyVar fox
set code [lindex $line 0] ;#Set code to retrieve xCode from text file
set morph [lindex $line 2] ;#Set morph to the actual value from text file
With a conditional if statement
if { $code eq "pCode" } {
puts "I found a picture which is $morph" ;#outputs MyVar
}
However, I am struggling to recall the value of the variable defined within the text file.
The closest, I've had is
set animal [eval $$morph] ;#references MyVar value which is fox as previously defined
But I get
invalid command name "fox"
Ultimately, I just want to output or save to a variable "fox" from the defined MyVar but cant seem to find the right combination.
I'm sure this is so simple but .. stuck.
Upvotes: 0
Views: 221
Reputation: 682
The set
procedure accepts 1 or 2 parameters and returns the actual value for variable: 1) name of variable 2) value to be assigned to variable. If the second parameter is not specified, then the variable will not be changed. So, the simplest solution is to call the set
procedure with only variable name. In your example:
set animal [set $morph]
Upvotes: 2