I am writing a tcl program which seems to be getting larger and larger. I am now facing a dilemma that my quick and dirty use of globals is getting out of hands.
I am using them since I do not seem to be able to use upvar or something correctly.
Even the example from tcl book
proc incr { varName {amount 1}} {
upvar 1 $varName var
if {[info exists var]} {
set var [exor $var + $amount]
} else {
set var $amount
}
return $var
}
set a 5
puts [incr $a 2]
puts $a
is not working for me. Variable a remains in value 5 and the incr procedure actually returns 2.
More to the point what I am looking for is something with multiple variables and without return, I want to change the value of the variables within procedure.
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"
Without those now uncommented set a_sub $a commands I get an error message about unknown variables, with those commands I still do not get what I want.
I am using them since I do not seem to be able to use upvar or something correctly.
Even the example from tcl book
proc incr { varName {amount 1}} {
upvar 1 $varName var
if {[info exists var]} {
set var [exor $var + $amount]
} else {
set var $amount
}
return $var
}
set a 5
puts [incr $a 2]
puts $a
is not working for me. Variable a remains in value 5 and the incr procedure actually returns 2.
More to the point what I am looking for is something with multiple variables and without return, I want to change the value of the variables within procedure.
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"
Without those now uncommented set a_sub $a commands I get an error message about unknown variables, with those commands I still do not get what I want.