Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

best way to save and restore TCL ARRAYS ?

3,068 views
Skip to first unread message

Mel

unread,
May 6, 2005, 2:01:24 PM5/6/05
to
i have an in memory array and need to do a save and restore on program entry
and exit. what is the best way to do this ?

any code example is highly appreciated


st...@uscohens.com

unread,
May 6, 2005, 2:17:53 PM5/6/05
to
You can certainly use something like:

set outFile [open myFile.txt w]
puts $outFile [list array set myArray [array get myArray]]
close $outFile

Then simply source myFile.txt to reload the array.

Steve

Aric Bills

unread,
May 6, 2005, 2:17:41 PM5/6/05
to

There's always:

#save
set file [open $filename w]
puts $file [array get myarray]
close $file

#restore
set file [open $filename r]
array set myarray [gets $file]
close $file

glennh

unread,
May 6, 2005, 5:23:30 PM5/6/05
to

You could use an sqlite database table to store the data rather than an
array.

Glenn

Mel

unread,
May 9, 2005, 8:58:02 AM5/9/05
to
but, i dont know columns to create a table for it. array elements are set &
unset by users at will.

if you have a solution to this maybe you can drop me a line or two of sqlite
insert/update/whatever you may have

thanks for your help
"glennh" <gl...@nospam.glennh.com> wrote in message
news:1115414643.f00217ba45e92e541e52b59268da416c@teranews...

Arjen Markus

unread,
May 9, 2005, 9:08:27 AM5/9/05
to
Mel wrote:
>
> but, i dont know columns to create a table for it. array elements are set &
> unset by users at will.
>
> if you have a solution to this maybe you can drop me a line or two of sqlite
> insert/update/whatever you may have
>

Why not use this to save the array:

set outfile [open "somefile" w]
puts $outfile "array set myarray [list [array get myarray]]"
close $outfile

and this to restore it:

source "somefile"

?

Regards,

Arjen

d...@hwaci.com

unread,
May 10, 2005, 9:03:10 AM5/10/05
to
Arjen Markus wrote:
>
> Why not use this to save the array:
>
> set outfile [open "somefile" w]
> puts $outfile "array set myarray [list [array get myarray]]"
> close $outfile
>
> and this to restore it:
>
> source "somefile"
>

This technique is fast and easy. But the use of "source"
can be dangerous in some situations. If an advisary is able
to modify "somefile" then they will be able to crash your
program or (worse) insert a trojan horse into your program.
This may or may not be a problem, depending on what your
program does and how it is used.

A safer approach would be:

set out [open "somefile" w]
puts $out [array get myarray]
close $out

And

set in [open "somefile"]
array set myarray [read $in]
close $in

Using SQLite gives you the added advantage that the updates
to somefile are atomic. If your program crashes or your
computer loses power in the middle of saving the array, the
content of "somefile" is not corrupted when SQLite is used.
SQLite will also make sure that two separate instances of
the program do not try to write "somefile" at the same time.
If these factors are a concern, then you can use SQLite to
store your array as follows:

sqlite3 db "somefile"
catch {db eval {CREATE TABLE myarray(name,value)}}
db eval BEGIN
foreach {name value} ]array get myarray] {
db eval {INSERT INTO myarray VALUES($name,$value)}
}
db eval COMMIT
db close

Then to restore:

sqlite3 db "somefile"
array set myarray [db eval {SELECT * FROM myarray}]
db close

0 new messages