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 

variable backup in file..

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    egghelp.org community Forum Index -> Archive
View previous topic :: View next topic  
Author Message
GodOfSuicide
Master


Joined: 17 Jun 2002
Posts: 463
Location: Austria

PostPosted: Tue Jun 03, 2003 12:50 pm    Post subject: variable backup in file.. Reply with quote

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


Joined: 23 Sep 2001
Posts: 3914
Location: Liverpool, England

PostPosted: Tue Jun 03, 2003 2:42 pm    Post subject: Reply with quote

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
View user's profile Send private message Yahoo Messenger MSN Messenger
stdragon
Owner


Joined: 23 Sep 2001
Posts: 959

PostPosted: Tue Jun 03, 2003 3:14 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
ppslim
Revered One


Joined: 23 Sep 2001
Posts: 3914
Location: Liverpool, England

PostPosted: Tue Jun 03, 2003 3:19 pm    Post subject: Reply with quote

No, thats why I used quotes and not brackets.

Damn, you can't have been away that long Rolling Eyes
_________________
PlusNet Supported Customer - Low cost UK ISP services
Back to top
View user's profile Send private message Yahoo Messenger MSN Messenger
stdragon
Owner


Joined: 23 Sep 2001
Posts: 959

PostPosted: Tue Jun 03, 2003 3:48 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
ppslim
Revered One


Joined: 23 Sep 2001
Posts: 3914
Location: Liverpool, England

PostPosted: Tue Jun 03, 2003 4:49 pm    Post subject: Reply with quote

True.

However, namespaces and the upvar commands are the two I am trying to work on, upvar I have never had use for.
_________________
PlusNet Supported Customer - Low cost UK ISP services
Back to top
View user's profile Send private message Yahoo Messenger MSN Messenger
GodOfSuicide
Master


Joined: 17 Jun 2002
Posts: 463
Location: Austria

PostPosted: Thu Jun 05, 2003 10:42 am    Post subject: Reply with quote

does this also work for multiple arrays (like myvar(name,date)) ?
Back to top
View user's profile Send private message
stdragon
Owner


Joined: 23 Sep 2001
Posts: 959

PostPosted: Thu Jun 05, 2003 8:19 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    egghelp.org community Forum Index -> Archive 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