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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Combining variables 1

Status
Not open for further replies.

shussai2

Programmer
Aug 28, 2003
42
CA
I know I have asked a similar question before but this one is a little tricky:
Given:

global chip
global reg
global reg${chip}x${reg}

What I want to do is display the contents of
reg${chip}x${reg} by looping since chip is from 1 to 2 and reg is from 0 to 31. Here is what I have:

set c 1
while {$c <= 2} {
set i 0
while {$i <= 31} {
puts $fileId &quot;R $c $i $reg${c}x${i}&quot; -----PROBLEM
incr i
}
incr c
}
 
1/ Tcl does only one substitution round and your code needs two rounds.
2/ It should be better you use an array.

1/ Tcl substitution
When interpreting a command, Tcl first does variables substitutions (replaces $x by its value) and embedded scripts evaluations (replaces [func] by the returned value).
Code:
  set a a
  set b b
  set ab value
  puts $a$b
-> ab
What you want is:
Code:
  puts $a$b (<- wrong syntax)
-> value    (<- wanted value)
You can obtain the wanted value with:
Code:
  puts [set $a$b]
-> value
This because Tcl translates [set $a$b] to [set ab] and obtains the desired value.

2/ Using an array
When you find yourself generating the name of a variable, it's time to see if an array is not a good solution:
Code:
  set reg(1,0) reg1,0
  set chip 1
  set reg 0
  puts $reg($chip,$reg)
-> reg1,0
A page of interest:
HTH

ulis
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top