Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

about doulbe subsititution

Status
Not open for further replies.

jiankunx

Technical User
Aug 16, 2002
4
CA
I remembered somewhre in the forum about double substitution , but could not find it. Here is my problm,

I used a global variable to store variable names which I will use in a function.but I have problem to get the value of the variable.

set a1 1
set a2 2
set var(0) a1
set var(1) a2

proc printvar {varNum} {

global var

puts "$$var($varNum)"

}

when I ran "printvar 0", it prints "$a1" instread of 1. How should I solve this problem.

Thanks
 
Two ways:
Globally:
proc printvar {varNum} {
global var
puts "$var($varNum)"
}

Upvar:
proc printvar {n varNum} {
upvar #0 $n loc
puts $loc($varNum)
}

String tricks won't work with tcl arrays they need to
be explicitly introduced at stack level 'n'.

What you meant to do:

proc printvar {n nx} {
uplevel #0 "puts [subst -novariable $$n]($nx)"
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top