| View previous topic :: View next topic |
| Author |
Message |
Nimos Halfop
Joined: 20 Apr 2008 Posts: 80
|
Posted: Fri Jun 06, 2008 7:15 pm Post subject: Make a string survive rehashing... |
|
|
| how can I write something into a file, and reload it after rehashing? |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Fri Jun 06, 2008 9:27 pm Post subject: |
|
|
There are a few various techniques to accomplish that. If your intention is to restore the value of a variable, you might be able to use something like this:
| Code: | proc savevars {file varlist} {
set fId [open $file "WRONLY CREAT TRUNC"]
foreach var $varlist {
upvar #0 $var tmp
puts $fId [list set $var $tmp]
}
close $fId
}
|
Then, all you have to do to save is call savevars with the file to store variables in, and the list of variables to save, and load the savefile as a normal script to restore:
| Code: | #save var1, var2, and var3 in myvars.save
savevars "./myvars.save" [list var1 var2 var3]
#Restore saved values
if {[file exists "./myvars.save"]} {source ./myvars.save}
|
_________________ NML_375, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Sat Jun 07, 2008 11:41 am Post subject: |
|
|
You could also save the variables automatically, using something like this.
In this case, you use register_savevar to tell the script which global variables to save.
| Code: | bind evnt - "prerehash" save_rehash
bind evnt - "prerestart" save_rehash
proc save_rehash {evnt} {
if {[info exists ::savevars]} {
savevars "./botname.savevars" $::savevars
}
}
proc register_savevar {var} {
if {!([info exists ::savevars] && [lsearch -exact $::savevars $var])} {
lappend ::savevars $var
}
}
|
The following modifications to savevars is also recommended, in order to handle non-existent variables.
| Code: | proc savevars {file varlist} {
set fId [open $file "WRONLY CREAT TRUNC"]
foreach var $varlist {
upvar #0 $var tmp
if {[info exists tmp]} {
puts $fId [list set $var $tmp]
}
}
close $fId
} |
_________________ NML_375, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
|