Forum OpenACS Q&A: Tcl question

Collapse
Posted by hau wan lin on
Can I know how to get a value of a variable by the following way? I have a variable called test1 and I set it to be 25

set test1 25

Instead of getting the value of test1 by $test1, I set another variable test to be "test1" and do something to get the value of test1. Can anyone show me how to do that? Thank you for your help

The attached won't work.

set number 1

set test [subst {test$number}]

set t [eval $test]

set whole_page "

$email $password

$test1

$t"

ns_return 200 text/html $whole_page

Collapse
2: Response to Tcl question (response to 1)
Posted by Dan Wickstrom on
set is your friend in this case:

set test [set test$number]

Collapse
3: Response to Tcl question (response to 1)
Posted by Michael A. Cleverly on
You want to do this:
set test1 25
set number 1
set test test$number
set t [set $test]
Now t will have the value of 25. It's easy to see what Tcl is doing if you drop to a shell and fire up tclsh:
% set test1 25
25
% set number 1
1
% set test test$number
test1
% set t [set $test]
25
[set] when it receives only one argument treats that argument as the name of a variable and returns the value of the variable (if it exists--or throws an error if it doesn't exist). See http://aolserver.com/docs/tcl/tcl8.3/TclCmd/set.htm.