GodOfSuicide wrote:credits for this code go to ppslim, slenox and all the others in the "backup array" thread
An alternative way would be to write it to the file as tcl code creating the array and just 'source the.file' (making the loading a bit faster I imagine) Where's that thread btw?
Edit: here's some generic code for variable storage:
# usage: var2str <qualified name> ?output name? ?pretty array boolean?
# the pretty array option is to choose between 'array set' or multiple 'set's for arrays with one or more elements
proc var2str {var {name {}} {prettyArray 0}} {
if {$name=={}} {set name $var}
if {[set var [uplevel 1 [list namespace which -variable $var]]]==""} {
error "No such variable "$name""
}
upvar 1 $var v
if {[info exists v]} {
if {[array exists v]} {
if {$prettyArray&&[array size v]} {
foreach {ele val} [array get v] {
lappend out [list set ${name}($ele) $val]
}
join [lsort -dict $out] \n
} {
list array set $name [array get v]
}
} {
list set $name $v
}
} {
list variable $name
}
}
# usage: storeVar <variable name> <file name> ?file access mode?
proc storeVar {var file {mode w}} {
set f [open $file $mode]
puts $f [var2str $var $var 1]
close $f
}