I want to save the data that is held in an array to a file on disk.
After rehash, I want to be able to restore that array, by reading the file.
Searched this forum.
Found some other threads that looked exactly like what I needed.
http://forum.egghelp.org/viewtopic.php? ... ckup+array
see post by GodOfSuicide, Oct 8 .
and
http://forum.egghelp.org/viewtopic.php? ... ckup+array
post by stdragon
I wrote a little script, to experiment with this.
It would write the file.
It would not read back from the file.
Error was:
Tcl error [readfile]: list must have an even number of elements
Examined file on hard disk.
In it were the pairs. Element name/data
However, the first character in the file was a { , and the last char was a} .
Removed them, and it now seems to read back the file, into array.
To get script to write the file, without the leading and trailing curly braces,
changed:
puts $fp list [array get $arr_name]
to
puts $fp [array get $arr_name]
Yet in one of those threads, it seemed like the poster was specifically saying that list command was needed.
On top of this, I've gone and looked up the uplevel command. I don't *think* I need that, so I'm not using it.
I've gotten myself confused, and could use some direction here.
1.) Is that an error in those old threads? ... that list command in
there?
2.) Would this be ok? :
Code: Select all
# basic proc to write from array to file
set fp [open ${testfilesdir}${testfile} w]
puts $fp [array get $arr_name]
close $fp
Code: Select all
#basic proc to read from file, to array
set fp [open ${testfilesdir}${testfile} r]
list array set $arr_name [read -nonewline $fp]
close $fp
Thanks