-Q9C- pass an array into a proc?

From: -IX-  How, in Tcl, can I XXX:

A9C. Use upvar rather than try to use global variables when possible.  If
the function is event driven, you are forced to use global variables.


# print elements of an array
proc show_array arrayName {
    upvar $arrayName myArray

    foreach element [array names myArray] {
       puts stdout "${arrayName}($element) =  $myArray($element)"
    }
}

set arval(0) zero
set arval(1) one
show_array arval


To return an array from a procedures, just take the array name in as an
argument, as above.  Any changes you make in the array will be made in
the parent's array as well.

Extended Tcl introduces a concept called keyed lists which are arrays
made out of lists of key-value pairs and can be passed by value to routines,
over networks, etc.
Go Back Up

Go To Previous

Go To Next