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

proc and upvar problem

Status
Not open for further replies.

Juge

Technical User
Jan 21, 2015
1
DE
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top