1
votes

If I define the following list:

set ::listofvariablesn [list {"inputfile" "Input file"}]
set ::inputfile "Somefile or numbers"

I can check if the variable ::inputfile exists using:

foreach a $::listofvariablesn {
   if {[info exists ::[lindex $a 0]]} {
      puts "Ok" 
   }
}

But I cannot access the value of ::inputfile using:

foreach a $::listofvariablesn {   
    if {[checkvl "Input file" $::[lindex $a 0] 1 0 0 0]} { 
        puts "Ok" 
    }
}

I got the error: can't read "::": no such variable

Is there a way to get this substitution done? I mean turn $::[lindex $a 0] into $::inputfile so I can access the value of ::inputfile

Thanks a lot

2

2 Answers

2
votes

The $ is a shortcut, and can't do double substitution. Use the set command:

if {[checkvl "Input file" [set ::[lindex $a 0]] 1 0 0 0]} { 
0
votes

Apart from the longhand form of $, the one-argument form of the set command, there is also the option to create an alias of the variable (with a known name) using upvar 0. For example:

upvar 0 $a b
puts "exists: [info exists b]" 
puts "value: $b" 

Anything you do to the variable b (except changing the upvar) will be performed on the variable that was named by $a.