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 

Reading and writing to files

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


Joined: 18 Feb 2006
Posts: 2
Location: Bucharest

PostPosted: Wed Feb 22, 2006 5:39 am    Post subject: Reading and writing to files Reply with quote

Hy all! This is my first post here, I just downloaded eggdrop and I'm a newbie to TCL scripting. I scripted in mIRC scripting before but TCL brings new and harder topics to understand. Well what I need (and could not find out) is how can I overwrite a line in a file without the need of reading the previous lines or using a temporary file. Here is my code that I have now:
Code:

bind part - * data_seen

proc data_seen {nick uhost hand chan text} {
   set file1 [open "DSotM/Temp/Seen.txt" r]
   set file2 [open "DSotM/Temp/Tmp/Seen.txt" w]
   set host [lindex [split $uhost @] 1]
   set date [clock format [clock seconds] -format "%D"]
   set time [clock format [clock seconds] -format "%T"]
   set contor 0
   while {[gets $file1 line] >= 0 && $contor == 0} {
      if {[lindex $line 0] == $nick} {
         puts $file2 "$line"
      }
   }
   puts $file2 "$nick $host $date $time"
   close $file1
   close $file2
   file copy -force "DSotM/Temp/Tmp/Seen.txt" "DSotM/Temp/Seen.txt"
   file delete "DSotM/Temp/Tmp/Seen.txt"
}


Now this script is opening the original file, $file1, read lines from it and if the nick that parted the channel it's not the nick in the file, it writes that line to a temporary file. Then at the end it appends the nick, host, date and time variables to that temp file and then it overwrites the original file. Now this works for small files, but with files larger than 1000 nicks (and that is very possible for a crowded channel) this proccess of reading and writing takes veeeeery much time. How can insert that $nick $host $date $time at a defined line in the original file, so I won't have 2 lines with the same nick on them?

Thank you in advance, Christmas_.

LE: Sorry for that code formatting, how do I use that code tags? Embarassed
_________________
The sun is the same in a relative way but you're older
And shorter of breath and one day close to death.
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
Sir_Fz
Revered One


Joined: 27 Apr 2003
Posts: 3793
Location: Lebanon

PostPosted: Wed Feb 22, 2006 7:22 am    Post subject: Reply with quote

You can read the file into a list, remove the line you want using [lreplace] and then save back.
_________________
Follow me on GitHub

- Opposing

Public Tcl scripts
Back to top
View user's profile Send private message Visit poster's website
Christmas
Voice


Joined: 18 Feb 2006
Posts: 2
Location: Bucharest

PostPosted: Wed Feb 22, 2006 10:12 am    Post subject: Reply with quote

Well I created a list, each element of it contains a line of the file and if $nick exists in the list, that element gets replaced with the new info for $host, $date and $time, or else the $nick, $host, $date and $time are appended to the list as a new element at the end. Well if I write this list to a file (element by element) it take the same time as before...
_________________
The sun is the same in a relative way but you're older
And shorter of breath and one day close to death.
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
Sir_Fz
Revered One


Joined: 27 Apr 2003
Posts: 3793
Location: Lebanon

PostPosted: Wed Feb 22, 2006 5:35 pm    Post subject: Reply with quote

Maybe this might help. See code by user.
_________________
Follow me on GitHub

- Opposing

Public Tcl scripts
Back to top
View user's profile Send private message Visit poster's website
demond
Revered One


Joined: 12 Jun 2004
Posts: 3073
Location: San Francisco, CA

PostPosted: Thu Feb 23, 2006 1:35 am    Post subject: Reply with quote

you just downloaded eggdrop and you are already trying to write nontrivial seen script? wow

I think you are totally confused about the basic principles of saving data from eggdrop scripts; you should load data on script's startup, do all of your data handling in memory and only periodically write to file
_________________
connection, sharing, dcc problems? click <here>
before asking for scripting help, read <this>
use [code] tag when posting logs, code
Back to top
View user's profile Send private message Visit poster's website
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