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.

[Solved] Write multiple lines to file.

Help for those learning Tcl or writing their own scripts.
Post Reply
t
tessa1
Halfop
Posts: 49
Joined: Mon Apr 18, 2005 12:51 pm
Location: Germany

[Solved] Write multiple lines to file.

Post by tessa1 »

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

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.
User avatar
SpiKe^^
Owner
Posts: 831
Joined: Fri May 12, 2006 10:20 pm
Location: Tennessee, USA
Contact:

Post by SpiKe^^ »

try changing w+ to a...

Code: Select all

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
.
t
tessa1
Halfop
Posts: 49
Joined: Mon Apr 18, 2005 12:51 pm
Location: Germany

Post by tessa1 »

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.
w
willyw
Revered One
Posts: 1205
Joined: Thu Jan 15, 2009 12:55 am

Post by willyw »

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

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.
t
tessa1
Halfop
Posts: 49
Joined: Mon Apr 18, 2005 12:51 pm
Location: Germany

Post by tessa1 »

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

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)
w
willyw
Revered One
Posts: 1205
Joined: Thu Jan 15, 2009 12:55 am

Post by willyw »

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. :)
t
tessa1
Halfop
Posts: 49
Joined: Mon Apr 18, 2005 12:51 pm
Location: Germany

Post by tessa1 »

Yes! Thats it!

Many thanks :)
Post Reply