#!/usr/bin/tclsh # # Simple script that dumps the names and values of all variables that # are currently in scope. # # Iterate over all visible variables. foreach var_name [lsort [info vars]] { # Dereference the variable name. upvar 0 $var_name var # Print the variable name. puts $var_name # Array. if {[array exists var]} { puts " as array:" foreach k [lsort [array names var]] { puts " ${var_name}($k) => $var($k)" } continue } # # By design, strings, lists, and dictionaries are virtually # indistinguishable. For example, their is no way to distinguish # between the string "Hello, World!", a the two-elment list, or a # dictionary with one key-value pair. So, we just print the value # interpreted as each type. # # As string. puts " as string:" puts " $var" # As list. catch { set i 0 foreach e $var { if {$i == 0} { puts " as list:" incr i } puts " $e" } } # As dictionary. catch { set i 0 foreach k [dict keys $var] { if {$i == 0} { puts " as dict:" incr i } puts " $k => [dict get $var $k]" } } }