2

I'm having some issues with Tcl. I have a variable that has a string in it. Butt now I want this string to be the name of a next variable.

I have found some similar questions on the web, but these are all about placing the value from a variable into another variable. not using it as the name of the variable.

Here is a sample code to help explaining:

    foreach key [array names ::openedFiles] {
    puts $::openedFiles($key)
    set filename [file tail $::openedFiles($key)]
    set parts [split $filename .]
    set name [lindex $parts 0]

    puts $name

    $L1 create cell $name

    set "value of $name" [ layout create $::openedFiles($key) -dt_expand -log LUN.log]

So bassicly it has to do the following. The array has some path string in it. I get only the name of the file out of the path without the file extension.

Butt then I want to create a variable "cell" that is the value of "$name". So when the filename is "Test", the value of $name will be "Test" and I want to do the last line like this

     set Test [ layout create $::openedFiles($key) -dt_expand -log LUN.log]

So that the value of $name will be the name of the new variable. And so I can creat a variable with the name of all the values in the array.

Any help or pointers would be great!

Thank you very much!

Jerre_111
  • 79
  • 1
  • 9
  • 1
    Doesn't `set $name [ layout create $::openedFiles($key) -dt_expand -log LUN.log]` work? – Jerry May 24 '13 at 11:55
  • No apperantly, cause when I want do it like that and I go further with the next code : `set cell_top [$name topcell]` I get an error that he can't find the option topcell for $name. BUt when I just use a name without a variable it does the trick so... – Jerre_111 May 24 '13 at 11:58
  • Have I got this right? You have a variable, say `a` which contains the name of another varioable, say `b` and you want to assign into `b` by referring to `a`. – nurdglaw May 24 '13 at 12:07
  • No, not quite ( unless I didn't understand you correct, than its my fault). I have a var `a` which contains the value `b`. And then I want to create a new variable named `b`. – Jerre_111 May 24 '13 at 12:11

2 Answers2

4

The correct solution is to use $name as first parameter to set

set name "foo"
set $name "bar"
puts $foo ;# -> bar

But if you try to use $name it will yield foo, not bar (Which is what you do in your code according to the comments. I don't know why you need a variable whose name you don't know, but anyway:

puts [set $name] ;# -> bar

will give you the correct thing. The same works with objects:

set topcell [[set $name] topcell]

So I have to ask you: what do you want to do with the dynamic named variable then?

Johannes Kuhn
  • 14,778
  • 4
  • 49
  • 73
  • I am writing a script that is functioning as a macro for a viewing programma for electronic desings. I use the dynamic variable for creating a layout. But the layout gets the name of the variable automaticly. So that's why i want to give every variable a different dynamic name – Jerre_111 May 24 '13 at 12:17
  • 1
    Most cases where you think you want double-dereferencing are actually better served with using an element of an array. – Donal Fellows May 24 '13 at 14:22
0

I'm thinking you want upvar to create an "alias" variable name

$ tclsh
% set name Test
Test
% upvar 0 cell $name
% set cell 42
42
% puts $Test
42
glenn jackman
  • 238,783
  • 38
  • 220
  • 352