LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   tcl procedures and changing variable values inside a procedure (https://www.linuxquestions.org/questions/programming-9/tcl-procedures-and-changing-variable-values-inside-a-procedure-4175531691/)

Jykke 01-21-2015 01:19 AM

tcl procedures and changing variable values inside a procedure
 
I am trying to avoid use of global variables but I feel like having two left hands with Tcl.

I now tried a simple procedure, hacked it from some examples - it does not work...

Code:

proc incr_a_b_upvar { a b c} {
 puts "a = $a"
 puts "b = $b"
 puts "c = $c"
 upvar 1 $a a_sub
 upvar 1 $b b_sub
 set a_sub $a
 set b_sub $b
 
 puts "a_sub = $a_sub"
 puts "b_sub = $b_sub"
 
 set a [expr {$a + $c}]
 set b [expr {$b + $c}]
}
set a 2
set b 3
set c 1
incr_a_b_upvar $a $b $c
puts "a = $a"
puts "b = $b"
puts "c = $c"

In similar examples it should actually work without those
two set commands inside the procedure
Code:

set a_sub $a
 set b_sub $b

but without them I get:
Quote:

a = 2
b = 3
c = 1
can't read "a_sub": no such variable
while executing
"puts "a_sub = $a_sub""
(procedure "incr_a_b_upvar" line 10)
invoked from within
"incr_a_b_upvar $a $b $c"
(file "incr_a_b.tcl" line 30)
So what gives?


All times are GMT -5. The time now is 04:50 PM.