| View previous topic :: View next topic |
| Author |
Message |
tessa1 Halfop
Joined: 18 Apr 2005 Posts: 49 Location: Germany
|
Posted: Sun Sep 22, 2013 12:18 am Post subject: [Solved] Write multiple lines to file. |
|
|
Hi
I want to write the stats data of all 6 servers into a file.
e.g:
server1.irc.net:1268
server2.irc.net:724
server3.irc.net:954
...
But my script writes only the last server number six into the file. What can I do?
Here is my script:
| Code: | bind pub - !uptime stats:cmd:uptime
proc stats:cmd:uptime {nick uhost handle channel text} {
global server_stats
putserv "STATS u server1.irc.net"
putserv "STATS u server2.irc.net"
putserv "STATS u server3.irc.net"
putserv "STATS u server4.irc.net"
putserv "STATS u server5.irc.net"
putserv "STATS u server6.irc.net"
}
bind raw - 242 stats:raw:242
proc stats:raw:242 {from keyword text} {
global server_stats
set server_stats(text) "$from:[lindex [split [lrange $text 3 end] " "] 0]"
set fd [open "uptimefile" w+]
puts $fd $server_stats(text)
close $fd
}
unset -nocomplain server_stats(text) |
Thank you for help 
Last edited by tessa1 on Sun Sep 22, 2013 2:12 pm; edited 1 time in total |
|
| Back to top |
|
 |
SpiKe^^ Owner

Joined: 12 May 2006 Posts: 792 Location: Tennessee, USA
|
Posted: Sun Sep 22, 2013 9:58 am Post subject: |
|
|
try changing w+ to a...
| Code: | | set fd [open "uptimefile" a] |
_________________ SpiKe^^
Get BogusTrivia 2.06.4.7 at www.mytclscripts.com
or visit the New Tcl Acrhive at www.tclarchive.org
. |
|
| Back to top |
|
 |
tessa1 Halfop
Joined: 18 Apr 2005 Posts: 49 Location: Germany
|
Posted: Sun Sep 22, 2013 11:52 am Post subject: |
|
|
Spike, yes! I know that. But "a" doesn't overwrite the file like "w"
It adds a new line to this file. I need this file to read it out with php and publish it on our homepage. |
|
| Back to top |
|
 |
willyw Revered One
Joined: 15 Jan 2009 Posts: 1175
|
Posted: Sun Sep 22, 2013 12:13 pm Post subject: |
|
|
We know what happens if you use +w. You get only the last one saved to the file. That's because +w is re-writing the file.
Spike's solution does cure that. However it creates a never ending file.
With your latest clarification, it sounds like you want both - a new file every time, that contains all six lines.
Would the solution be:
Do what Spike said, AND add a new line to your script.... one that deletes the old file, first?
Reference:
http://www.tcl.tk/man/tcl8.5/TclCmd/file.htm#M12
| Code: |
proc stats:raw:242 {from keyword text} {
global server_stats
file delete uptimefile
.... rest of your script text here .....
|
Something like that, anyway...
I hope this helps. |
|
| Back to top |
|
 |
tessa1 Halfop
Joined: 18 Apr 2005 Posts: 49 Location: Germany
|
Posted: Sun Sep 22, 2013 12:36 pm Post subject: |
|
|
Ok, this is my script now.
There are no errors in the partyline.
But the script writes only the last server.
I rehashed an restarted the Bot.
Without deleting the file, the script writes all lines.
| Code: | bind pub - !uptime stats:cmd:uptime
proc stats:cmd:uptime {nick uhost handle channel text} {
global server_stats
putserv "STATS u server1.irc.net"
putserv "STATS u server2.irc.net"
putserv "STATS u server3.irc.net"
putserv "STATS u server4.irc.net"
putserv "STATS u server5.irc.net"
putserv "STATS u server6.irc.net"
}
bind raw - 242 stats:raw:242
proc stats:raw:242 {from keyword text} {
global server_stats
file delete uptimefile
set server_stats(text) "$from:[lindex [split [lrange $text 3 end] " "] 0]"
set fd [open "uptimefile" a]
puts $fd $server_stats(text)
close $fd
}
unset -nocomplain server_stats(text) |
|
|
| Back to top |
|
 |
willyw Revered One
Joined: 15 Jan 2009 Posts: 1175
|
Posted: Sun Sep 22, 2013 12:56 pm Post subject: |
|
|
Every time proc stats:raw:242 is triggered by the raw bind, the file is being deleted.
Thus, only the last one is being saved.
See what I mean?
My mistake. Sorry.
Try moving this line:
file delete uptimefile
from where it is currently, to before the first putserv line in
proc stats:cmd:uptime
That way, the file will only be deleted once - when you issue the
!uptime
command. Let's hope this solves it.  |
|
| Back to top |
|
 |
tessa1 Halfop
Joined: 18 Apr 2005 Posts: 49 Location: Germany
|
Posted: Sun Sep 22, 2013 1:13 pm Post subject: |
|
|
Yes! Thats it!
Many thanks  |
|
| Back to top |
|
 |
|