0

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.

1 Answer 1

2

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]
Sign up to request clarification or add additional context in comments.

1 Comment

Wow, how embarrassing. Thank you for replying and descriptive text.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.