INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

proc and upvar problem

proc and upvar problem

(OP)
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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close