Version 5 of How to pass arrays

Updated 2002-04-16 19:25:20

[Explain what's meant.]


There are two choices for passing arrays both "into" and "out of" proc-s:

  • serialization, generally into a list with "array get ..."
  • local proxying of an external variable by use of upvar or occasionally uplevel

[Examples]

  • Array from pairlist: array set A {city Hamilton state TX zip 34567}
  • Array to pairlist: set L [array get A]
  • Copying an array: array set B [array get A] [but we have to show explicitly how this relates to proc-to-proc communication]

Removing keys with empty values from an array: Note that the array name is passed in, associated with local variable arr. The unsetting of elements with empty value, done in arr, in fact happens in the caller's original array.

 proc cleanArray arrName {
     upvar 1 $arrName arr
     foreach key [array names arr] {
         if {$arr($key) == ""} {unset arr($key)}
     }
 }