TCL Training Series – Passing an array as procedure argument

A TCL array is an associative array. i.e. there is an (un-ordered) key-value pair in a TCL array. A simple list of elements as in a perl array or a C array is called a ‘list’ in TCL. If you want to pass an array, which s essentially a collection of variables, to a procedure in TCL, you can use the command `upvar` as explained earlier. You are passing a name to the procedure and then using it to access the array at the previous level.

proc passArray {argArray} {      
   upvar $argArray a             
   puts "$a(Tran), $a(Cap)"      
   puts [array names a]          
}                                
                                 
set arrayA(Tran) "0.1"           
set arrayA(Cap) "0.2"            
                                 
passArray arrayA                 

You should see the results:
`0.1, 0.2`
`Cap Tran`

While we are at it, let’s see the difference with passing a list. Here we pass a variable to the procedure.

proc passList {argList} {         
   puts $argList                  
}                                 
                                  
set listA {a b c}                 
passList $listA                   

1 comment on “TCL Training Series – Passing an array as procedure argument

Leave a Reply

Your email address will not be published. Required fields are marked *