Reputation: 79
This question is kind of a follow-up from my last question…
I'm having some problems on top of the solution from the answers there. Ok, so the story is like this. I'm trying to write a script in colaborating with a electronic design viewer program. The problem I'm experiencing is using my variables correctly in the syntax of the electronic program.
So basically the meaning is this. I read some designs in, save the different paths in an array and then read them out one by one. During this reading one by one I open them in the program.
The original code for opening the layout for the program is this:
set L1 [ layout create ]
$L1 create cell layout
$L1 create cell ONE
$L1 create cell TWO
set ONE [ layout create /users/jerre_111/public/imec_logo_zwart_512.gds -dt_expand -log LUN.log]
set ONE_top [$ONE topcell]
set TWO [ layout create /users/jerre_111/public/logo-rood.gds -dt_expand -log KTH.log]
set TWO_top [$TWO topcell]
$L1 import layout $ONE FALSE overwrite
$L1 import layout $TWO FALSE overwrite
$L1 create ref ONE $ONE_top 0 0 0 0 1
$L1 create ref TWO $TWO_top 0 0 0 0 1
$L1 create ref build ONE 0 0 0 0 1
$L1 create ref build TWO 207500 0 0 0 1
So, this is the original code. ONE
and TWO
are both two different layouts I open in the programm.
This is how it has to work in my script: I want to replace ONE and TWO dynamicly by the name of the file I open. So every time, I get the path out the array and then get the name out of the path. Then I want to use the name of the file instead of ONE
and TWO
. I'm trying it like this:
set L1 [ layout create ]
$L1 create cell build
foreach key [array names ::openedFiles] {
set filename [file tail $::openedFiles($key)]
set parts [split $filename .]
set name [lindex $parts 0]
$L1 create cell $name
set $name[ layout create $::openedFiles($key) -dt_expand -log LUN.log]
set cell_top [[set $name] topcell]
$L1 import layout [set $name] FALSE overwrite
$L1 create ref $name $cell_top 0 0 0 0 1
$L1 create ref build $name 0 0 0 0 1
}
So every time I go through the loop I get one item out of the array. and then I dynamiclly give them the names. Butt I'm having some problems reffering to $ONE
or ONE
like in the original code... I'm trying to replace the ONE
by the value of $name
...
I hope you understand my problem and someone can help me.
Upvotes: 1
Views: 161
Reputation: 137567
For this code, you probably want to use an aliased variable.
# Compute the name of the variable to use as in your code...
# I assume that the name is what $name produces
upvar 0 $name ALIAS
After that, you can then use ALIAS
or $ALIAS
where the original sample used ONE
or $ONE
; they behave identically (until the end of the stack frame or until you redefine the alias with another upvar 0
call).
$L1 create cell ALIAS
set ALIAS [layout create $::openedFiles($key) -dt_expand -log LUN.log]
set cell_top [$ALIAS topcell]
$L1 import layout $ALIAS FALSE overwrite
$L1 create ref ALIAS $cell_top 0 0 0 0 1
$L1 create ref build ALIAS 0 0 0 0 1
The only issue might be if the code retains a reference to the variable name. I'm guessing that this code doesn't, but some definitely does.
Alternatively use an array since things seem to expect a scalar. You could even use the array with an empty name:
$L1 create cell ($name)
set ($name) [layout create $::openedFiles($key) -dt_expand -log LUN.log]
set cell_top [$($name) topcell]
$L1 import layout $($name) FALSE overwrite
$L1 create ref ($name) $cell_top 0 0 0 0 1
$L1 create ref build ($name) 0 0 0 0 1
I don't really recommend using the empty name though; though it is going to be supported throughout the 8.* series of Tcl for sure, it's used in library packages (notably stooop), and we're not sure that we'll guarantee this as a supported feature in 9.0. An array with a one-letter name will be clearer IMO.
Upvotes: 3