This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

variable backup in file..

Old posts that have not been replied to for several years.
Locked
User avatar
GodOfSuicide
Master
Posts: 463
Joined: Mon Jun 17, 2002 8:00 pm
Location: Austria

variable backup in file..

Post by GodOfSuicide »

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: Select all

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.
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

Tcl arrays (they are infact hash lists) can be written to file easily.

Code: Select all


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
}
User avatar
stdragon
Owner
Posts: 959
Joined: Sun Sep 23, 2001 8:00 pm
Contact:

Post by stdragon »

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?
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

No, thats why I used quotes and not brackets.

Damn, you can't have been away that long :roll:
User avatar
stdragon
Owner
Posts: 959
Joined: Sun Sep 23, 2001 8:00 pm
Contact:

Post by stdragon »

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.
  • . 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
    • 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.
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

True.

However, namespaces and the upvar commands are the two I am trying to work on, upvar I have never had use for.
User avatar
GodOfSuicide
Master
Posts: 463
Joined: Mon Jun 17, 2002 8:00 pm
Location: Austria

Post by GodOfSuicide »

does this also work for multiple arrays (like myvar(name,date)) ?
User avatar
stdragon
Owner
Posts: 959
Joined: Sun Sep 23, 2001 8:00 pm
Contact:

Post by stdragon »

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: Select all

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 
}
Locked