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 

[Solved] Write multiple lines to file.

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


Joined: 18 Apr 2005
Posts: 49
Location: Germany

PostPosted: Sun Sep 22, 2013 12:18 am    Post subject: [Solved] Write multiple lines to file. Reply with quote

Hi Smile

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 Smile


Last edited by tessa1 on Sun Sep 22, 2013 2:12 pm; edited 1 time in total
Back to top
View user's profile Send private message
SpiKe^^
Owner


Joined: 12 May 2006
Posts: 792
Location: Tennessee, USA

PostPosted: Sun Sep 22, 2013 9:58 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
tessa1
Halfop


Joined: 18 Apr 2005
Posts: 49
Location: Germany

PostPosted: Sun Sep 22, 2013 11:52 am    Post subject: Reply with quote

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
View user's profile Send private message
willyw
Revered One


Joined: 15 Jan 2009
Posts: 1175

PostPosted: Sun Sep 22, 2013 12:13 pm    Post subject: Reply with quote

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
View user's profile Send private message
tessa1
Halfop


Joined: 18 Apr 2005
Posts: 49
Location: Germany

PostPosted: Sun Sep 22, 2013 12:36 pm    Post subject: Reply with quote

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
View user's profile Send private message
willyw
Revered One


Joined: 15 Jan 2009
Posts: 1175

PostPosted: Sun Sep 22, 2013 12:56 pm    Post subject: Reply with quote

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. Smile
Back to top
View user's profile Send private message
tessa1
Halfop


Joined: 18 Apr 2005
Posts: 49
Location: Germany

PostPosted: Sun Sep 22, 2013 1:13 pm    Post subject: Reply with quote

Yes! Thats it!

Many thanks Smile
Back to top
View user's profile Send private message
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