| View previous topic :: View next topic |
| Author |
Message |
Christmas Voice
Joined: 18 Feb 2006 Posts: 2 Location: Bucharest
|
Posted: Wed Feb 22, 2006 5:39 am Post subject: Reading and writing to files |
|
|
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?  _________________ 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 |
|
 |
Sir_Fz Revered One

Joined: 27 Apr 2003 Posts: 3793 Location: Lebanon
|
Posted: Wed Feb 22, 2006 7:22 am Post subject: |
|
|
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 |
|
 |
Christmas Voice
Joined: 18 Feb 2006 Posts: 2 Location: Bucharest
|
Posted: Wed Feb 22, 2006 10:12 am Post subject: |
|
|
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 |
|
 |
Sir_Fz Revered One

Joined: 27 Apr 2003 Posts: 3793 Location: Lebanon
|
|
| Back to top |
|
 |
demond Revered One

Joined: 12 Jun 2004 Posts: 3073 Location: San Francisco, CA
|
Posted: Thu Feb 23, 2006 1:35 am Post subject: |
|
|
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 |
|
 |
|