egghelp.org community Forum Index
[ egghelp.org home | forum home ]
egghelp.org community
Discussion of eggdrop bots, shell accounts and tcl scripts.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Make a string survive rehashing...

 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help
View previous topic :: View next topic  
Author Message
Nimos
Halfop


Joined: 20 Apr 2008
Posts: 80

PostPosted: Fri Jun 06, 2008 7:15 pm    Post subject: Make a string survive rehashing... Reply with quote

how can I write something into a file, and reload it after rehashing?
Back to top
View user's profile Send private message
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Fri Jun 06, 2008 9:27 pm    Post subject: Reply with quote

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
View user's profile Send private message
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Sat Jun 07, 2008 11:41 am    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help All times are GMT - 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Forum hosting provided by Reverse.net

Powered by phpBB © 2001, 2005 phpBB Group
subGreen style by ktauber