Search code examples
tclupvar

Tcl upvar to a variable in another proc


I have two procs: From MAIN I call another proc Glob. $Allforces is a list of lists.

proc ::MAIN {} {  
    # something
    ::Glob $AllForces
}

proc ::Glob {Input} {
    upvar $Input AllForces
    # do something
}

I get "No such variable" as an error for my attemt to upvar. So I tried:

upvar $InputMPCForces ::MAIN::AllForces

Then I get: "upvar won't create namespace variable that refers to procedure variable"

How can I access AllForces from MAIN in Glob by reference?


Solution

  • You need to call your proc like this:

    ::Glob AllForces
    

    that is, you pass the name of the variable, not its value.

    Then, in the proc, the upvar command will take the variable whose name is the value of the local variable input and make it available locally as AllForces