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

tcl tk variables 1

Status
Not open for further replies.

wjax

Programmer
Jul 22, 2003
2
FR
Hola que tal? Soy un programador de tcl, principiante.
Tengo el siguiente problema.
Quiero crear en tiempo de ejecucion una serie de variable de la forma
$ejemplo_x, donde x es un numero. Es facil crearlas, ya que pongo
"set ejemplo_$i valor" y se acabo. El problema, lo tengo al intentar acceder despues. No se como acceder, ya que por ejemplo, para hacer un puts "puts stdout $ejemplo_$i" no rula, y he probado ya otra pocas de cosas y tampoco.
Tienes idea de como podria hacer esto?
Gracias de antemano.
Un saludo

Hi. I am a debutant programmer.
I have this problem, I want to create in running time, a few variables, called $example_x where x will be a number from 1 to 9. So it is easy to create them ("set example_$i") but i have no clue of how to get their values, afterwards. Due that i cannot do "set other $example_$i".
So i need help, please.
Tell me if you know how.
Thanks a lot
 
The best way is to use an array, rather than a scalar variable. Here's a quick example:

[tt]% set i 3
3
% set example($i) "Here's a value"
Here's a value
% puts $example($i)
Here's a value[/tt]

Keep in mind that Tcl arrays aren't integer indexed. They're associative arrays, very much like a hash in Perl, or somewhat like a dictionary in Python. The index does not have to be an integer; you can use any string as an index -- including strings that look like integers, as I showed above. But you can also do this:

[tt]% set example(California) Sacramento
Sacramento
% set k California
California
% puts $example($k)
Sacramento[/tt]

If you insist on creating dynamically named scalar variable (which I advise against), you'll need to use the set command to force a second round of substitution, like this:

[tt]% set z 7
7
% set example_$z Blah
Blah
% puts [ignore][set example_$z][/ignore]
Blah[/tt]

- Ken Jones, President, ken@avia-training.com
Avia Training and Consulting, 866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
Hello again.
It is true, but i need to use "variable", because i am in a namespace. And it does not work arrays and "variables", together
 
Sure it does. The only restriction about using variable is that you can't set the value of individual array elements with a variable declaration (whereas it does allow initialization of scalar variable values). But you can use variable to declare a variable as a namespace variable, and then use that variable as an array. Here are some examples:

[tt]% namespace eval foo {
> variable bar
> variable i
> }
% namespace eval foo {
> set i 7
> set bar($i) "A value"
> puts $bar($i)
> }
A value
% puts $foo::bar(7)
A value
% proc foo::print {index} {
> variable bar
> puts $bar($index)
> }
% foo::print 7
A value
% set k 19
19
% set foo::bar($k) "Another value"
Another value
% foo::print 19
Another value[/tt]

- Ken Jones, President, ken@avia-training.com
Avia Training and Consulting, 866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top