arrayval

MGS [2003/09/05] - A quick proc that works like parray, but returns the value instead of printing to stdout.

 proc arrayval {array {pattern *}} {
   upvar $array a
   if { ![array exists a] } { error "\"$array\" isn't an array" }
   set lines [list]
   set max 0
   foreach name [array names a $pattern] {
     set len [string length $name]
     if { $len > $max } { set max $len }
   }
   set max [expr {$max + [string length $array] + 2}]
   foreach name [array names a $pattern] {
     set line [format %s(%s) $array $name]
     lappend lines [format "%-*s = %s" $max $line $a($name)]
   }
   return [join [lsort $lines] \n]
 }

 # example code
 text .t
 pack .t -expand 1 -fill both
 .t insert end [arrayval ::env]

See also: