2

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?

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
Lumpi
  • 2,697
  • 5
  • 38
  • 47

1 Answers1

4

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

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
nurdglaw
  • 2,107
  • 19
  • 37
  • This is correct. @Lumpi, you might also be interested in [this thread](http://stackoverflow.com/q/7299320/720999) to get a better idea about why that `$` is not needed when passing *the name* of a variable to a procedure. – kostix Jul 05 '13 at 15:52
  • I find it helps, mnemonically, to have arguments that will be `upvar`ed be called `SomethingName`. (In this case, `InputName`.) Then I would write `upvar $InputName Input` and have my variable by reference… – Donal Fellows Jul 06 '13 at 07:15