Arash sadr
Arash sadr

Reputation: 1

Saving an array in Tcl

I have a procedure in my code that is called in a loop. in this procedure, I want to store three variables in an array so at the end I can save the array in a text file. How can I do that? example: Main code:

for {set j 1} {$j<=[llength $Floor]} {incr j} {

myProc $matTag $j ........

}



proc myProc {....} {

 set Capacity($matTag) "$matTag $thetay $thetaP"

}

I have used this proc in my main code and at the end, if I have access to the array Capacity, I can save it. but this doesn't work within a proc.

Upvotes: 0

Views: 518

Answers (1)

Donal Fellows
Donal Fellows

Reputation: 137567

All Tcl variables, including arrays, are scoped to their procedure (or lambda term, or method) by default. If you want something else, you have to explicitly ask for it. There are several ways to do this (global, upvar, variable and namespace upvar); the one that you probably need is global, which (efficiently) makes one or more variables into global variables:

proc myProc {....} {
    global Capacity

    set Capacity($matTag) "$matTag $thetay $thetaP"
}

Conventionally, the global is at the top of the procedure, as shown above. It doesn't have to be, but code tends to be a bit more readable if it is.

Upvotes: 1

Related Questions