| View previous topic :: View next topic |
| Author |
Message |
GodOfSuicide Master

Joined: 17 Jun 2002 Posts: 463 Location: Austria
|
Posted: Tue Jun 03, 2003 12:50 pm Post subject: variable backup in file.. |
|
|
Hi..
i've got a quite big var (array), and i'd need to backup cos this data is lost if the bot crashes / restarts / etc
if there a way to write a var into a file and write it back again ?
like delphi
| Code: |
write :
for i := 0 to my_array.items.count -1 do
my_memo.lines.add(my_array.item[i])
get :
for i := 0 to my_memo.lines.count -1 do
my_array.item[i] := memo1.lines[i]
|
The array isn't myvar(1) myvar(2) but myvar(word) myvar(anotherword) and so on.
I'd like to dump this var into a txt file and if the bot restarts it should write the var based onto the data in this txt. |
|
| Back to top |
|
 |
ppslim Revered One
Joined: 23 Sep 2001 Posts: 3914 Location: Liverpool, England
|
Posted: Tue Jun 03, 2003 2:42 pm Post subject: |
|
|
Tcl arrays (they are infact hash lists) can be written to file easily.
| Code: |
proc write_array {name file} {
set fp [open $file w]
puts $fp [uplevel 1 "array get $name"]
close $fp
}
proc read_array {name file} {
set fp [open $file r]
uplevel 1 "array set $name [read -nonewline $fp]"
close $fp
}
|
_________________ PlusNet Supported Customer - Low cost UK ISP services |
|
| Back to top |
|
 |
stdragon Owner

Joined: 23 Sep 2001 Posts: 959
|
Posted: Tue Jun 03, 2003 3:14 pm Post subject: |
|
|
| ppslim, one problem with your code: in read_array you're passing the result of [read] to uplevel to be evaluated (since you used quotes). Maybe a better way is to use upvar? |
|
| Back to top |
|
 |
ppslim Revered One
Joined: 23 Sep 2001 Posts: 3914 Location: Liverpool, England
|
|
| Back to top |
|
 |
stdragon Owner

Joined: 23 Sep 2001 Posts: 959
|
Posted: Tue Jun 03, 2003 3:48 pm Post subject: |
|
|
ppslim give me some credit! I think you misunderstood what I said. Uplevel works just like eval, right? So it's not very safe to read in text and simply eval it. If there is a stray line with [blah blah] it will try to execute it. Now, if the person *only* uses write_array to write the file, it's not too bad, because it will escape those characters.
That said, I just realized the code doesn't even work. The 'array set' command expects a list as a single variable, e.g. [list name1 value1 name2 value2]. The code as stated, since it evaluates the *result* of [read] rather than the [read] itself, will make each thing a separate argument rather than a list element. This will call the command with too many arguments.
Perhaps I have been away too long if you've forgotten to put such things in a [list] construct rather than just quotes :) This is the same sort of problem that occurs when people say utimer "putserv $blah" rather than utimer [list putserv $blah].
Anyway, it's easier and more efficient to use the upvar command rather than have tcl create all these lists. |
|
| Back to top |
|
 |
ppslim Revered One
Joined: 23 Sep 2001 Posts: 3914 Location: Liverpool, England
|
|
| Back to top |
|
 |
GodOfSuicide Master

Joined: 17 Jun 2002 Posts: 463 Location: Austria
|
Posted: Thu Jun 05, 2003 10:42 am Post subject: |
|
|
| does this also work for multiple arrays (like myvar(name,date)) ? |
|
| Back to top |
|
 |
stdragon Owner

Joined: 23 Sep 2001 Posts: 959
|
Posted: Thu Jun 05, 2003 8:19 pm Post subject: |
|
|
In tcl, there are no multidimensional arrays. When you say blah(word1,word2) that is actually a single entry called "word1,word2", which is a perfectly valid name. If you say blah(word1) it will not return a new array (as a real multidimensional array would).
Anyway, I'll go ahead and update this code a bit, this should work, although I haven't tested it:
| Code: |
proc write_array {name file} {
set fp [open $file w]
puts $fp [uplevel 1 [list array get $name]]
close $fp
}
proc read_array {name file} {
set fp [open $file r]
uplevel 1 [list array set $name [read -nonewline $fp]]
close $fp
}
|
|
|
| Back to top |
|
 |
|