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.

Problem with textfiles

Help for those learning Tcl or writing their own scripts.
d
darton
Op
Posts: 155
Joined: Sat Jan 21, 2006 11:03 am

Problem with textfiles

Post by darton »

Hello!
When a bot in the channel writes one of the following sentences, my bot should save the playername with the number of MVPs and MostKills in a textfile. In the txt-file there should be displayed at first the playername, then the number of MVPs and then the number of MostKills.
Host: [BR] genaraln7 has been recommended by BlazeRegulator: MVP on C&C_Islands with 1000 points
Host: [BR] genaraln7 has been recommended by BlazeRegulator: Most Kills on C&C_Canyon with 17 frags
I began with making this script:

Code: Select all

bind pubm - * medals

proc medals {nick uhost hand chan arg} {
 set file scripts/medals.txt
 if {![file exists $file]} { close [open $file w] } 
 set l [split [read [set f [open $file]]][close $f] \n] 
 set n [lindex [split $arg] 2]
 set mvp 0
 set kills 0 
 if {[string match -nocase {*BlazeRegulator: Most Kills*} $arg]} {
 set kills [expr $kills + 1]
  lappend l "$n $mvp $kills"
  set f [open $file w]
  foreach le $l {
   if {$le != ""} { puts $f $le }
  }  
  close $f 
  }
  if {[string match -nocase {*BlazeRegulator: MVP*} $arg]} {
  set mvp [expr $mvp + 1]
   lappend l "$n $mvp $kills"
   set f [open $file w]
   foreach le $l {
   if {$le != ""} { puts $f $le }
  }  
  close $f 
  }
}
I hope it is right till now. At least it works, but there is something I have to add and I don't know how to do this:
1. The design of the txt-file is like this: "genaraln7 0 1 genaraln7 1 0" Is it possbile that there is a break before a new name begins?
2. If a person gets for example MVP and this person is already listed in the textfile, my bot should not begin a new line, but it should add it to the nick. How is this possible?
User avatar
De Kus
Revered One
Posts: 1361
Joined: Sun Dec 15, 2002 11:41 am
Location: Germany

Post by De Kus »

1. there should be a \n at the end of each entry. Try to open file with something else than notepad, add \r at the end of each line or fconfigure the file to use translation crlf (as output).
2. open file in r+, while !eof gets the file and lappend each line into a list. then lsearch -glob $list "$nickname *" and use the returned id to replace the entry with something else. then seek to 0 in the file and foreach puts it again.
De Kus
StarZ|De_Kus, De_Kus or DeKus on IRC
Copyright © 2005-2009 by De Kus - published under The MIT License
Love hurts, love strengthens...
d
darton
Op
Posts: 155
Joined: Sat Jan 21, 2006 11:03 am

Post by darton »

OK, with UltraEdit it is displayed correctly. But now there is still the other problem.
Here is an example:
Host: [BR] genaraln7 has been recommended by BlazeRegulator: MVP on C&C_Islands with 1000 points
Host: [BR] genaraln7 has been recommended by BlazeRegulator: Most Kills on C&C_Canyon with 17 frags
If this appears in the channel, my bot stores it like this:
genaraln7 0 1
genaraln7 1 0
But it should be stored like this: genaraln7 1 1
So if the nick is already in the textfile, my bot should add the number to this nick.

EDIT: I see, you edited your post, but can you please make a script? That would be nice.
Last edited by darton on Sat May 06, 2006 6:40 am, edited 1 time in total.
User avatar
De Kus
Revered One
Posts: 1361
Joined: Sun Dec 15, 2002 11:41 am
Location: Germany

Post by De Kus »

are you not familar enough with TCL to follow my description?

PS: I see, you edited as well :P
Last edited by De Kus on Sat May 06, 2006 6:44 am, edited 1 time in total.
De Kus
StarZ|De_Kus, De_Kus or DeKus on IRC
Copyright © 2005-2009 by De Kus - published under The MIT License
Love hurts, love strengthens...
d
darton
Op
Posts: 155
Joined: Sat Jan 21, 2006 11:03 am

Post by darton »

Not really :oops:
User avatar
De Kus
Revered One
Posts: 1361
Joined: Sun Dec 15, 2002 11:41 am
Location: Germany

Post by De Kus »

try following (untested):

Code: Select all

set medalsfile "scripts/medals.txt"
if {![file exists $medalsfile]} {
	catch {close [open $medalsfile w]}
}

bind pubm - * medals

proc medals {nick uhost hand chan arg} {
	set mvp [string match -nocase {*BlazeRegulator: MVP*} $arg]
	set kills [string match -nocase {*BlazeRegulator: Most Kills*} $arg]
	if {$mvp || $kills} {
		set name [lindex [split $arg] 2]
		set fd [open $::medalsfile r+]
		while {![eof $fd]} {
			lappend list [gets $fd]
		}
		if {[lindex $list end] == ""} {
			set list [lreplace $list end end]
		}
		if {[set le [lsearch -glob $list "$name *"]] != -1} {
			set line [lindex $list $le]
			incr mvp [lindex $line 1]
			incr kills [lindex $line 2]
			set list [lreplace $list $le $le [list $n $mvp $kills]]
		} else {
			lappend list [list $name $mvp $kills]
		}
		seek $fd 0
		puts -nonewline $fd [join $list \n]
		close $fd
	}
	return 0
}
I tried to alter your existing code in something that I would call a "proper syntax" :). Note especially that your original version read the whole file regradless, if the line was even worth opening it.

If you are worried about memory usage within the lreplace (since the data will temporarily be doubled), you can use the "k" function mentioned somewhere on the forums to empty $l, before filled with result of lreplace.

Edit: 13:12 typo
Last edited by De Kus on Sun May 07, 2006 6:23 am, edited 7 times in total.
De Kus
StarZ|De_Kus, De_Kus or DeKus on IRC
Copyright © 2005-2009 by De Kus - published under The MIT License
Love hurts, love strengthens...
d
darton
Op
Posts: 155
Joined: Sat Jan 21, 2006 11:03 am

Post by darton »

OK, first thanks for your work, but there is a mistake anywhere in your script.
If the textfile medals.txt does not exist, then there appears the following error:
Tcl error [medals]: couldn't open "scripts/medals.txt": no such file or directory
And if it exists:
Tcl error [medals]: can not find channel named "file9"
User avatar
De Kus
Revered One
Posts: 1361
Joined: Sun Dec 15, 2002 11:41 am
Location: Germany

Post by De Kus »

obviously "catch {close [open $file w]}" was supposed to be "catch {close [open $medalsfile w]}" and the first "close $f" was of course a copy&paste mistake and should not be there :D. I edited my post by that 2 changes.

PS: Intresting that you have a global var called "file" :D. You now have a file called "filex" or whatever is/was the content of $file in your bot dir ;).
De Kus
StarZ|De_Kus, De_Kus or DeKus on IRC
Copyright © 2005-2009 by De Kus - published under The MIT License
Love hurts, love strengthens...
d
darton
Op
Posts: 155
Joined: Sat Jan 21, 2006 11:03 am

Post by darton »

OK, if the file doesnt exist, an error appears, so first I have to create a new file so that it works.
Then if my bot should write something in the textfile, it makes 3 lines, the first and the last are empty and in the second line it writes the playername with mvp and kills.
And if in the textfile there is "genaraln7 1 1" and someone in the channel writes "Host: [BR] genaraln7 has been recommended by BlazeRegulator: MVP on C&C_Islands with 1000 points" this entry should be changed to "genaraln7 2 1", but that doesn't happen.
d
darton
Op
Posts: 155
Joined: Sat Jan 21, 2006 11:03 am

Post by darton »

Not possible??
User avatar
De Kus
Revered One
Posts: 1361
Joined: Sun Dec 15, 2002 11:41 am
Location: Germany

Post by De Kus »

sorry, didnt consider empty file and that the last line would always expent by an empty line. I changed code to discard the empty last line and dont put no more \n before EOF. However you will have to remove the first empty line by hand :D.

For the counting, you exressed yourself a bit misunderstanding, I thought you meant a boolan, not a counter :D. These simply incr statements should work... if my sleepy brain didnt make a total disaster :D.
De Kus
StarZ|De_Kus, De_Kus or DeKus on IRC
Copyright © 2005-2009 by De Kus - published under The MIT License
Love hurts, love strengthens...
d
darton
Op
Posts: 155
Joined: Sat Jan 21, 2006 11:03 am

Post by darton »

Tcl error [medals]: can't read "line": no such variable
Hm...what's that?
User avatar
De Kus
Revered One
Posts: 1361
Joined: Sun Dec 15, 2002 11:41 am
Location: Germany

Post by De Kus »

if you looked at the complete error message, you would have seen what var name there should be written :P. Thats why its recommned to use meaningfull var names.
De Kus
StarZ|De_Kus, De_Kus or DeKus on IRC
Copyright © 2005-2009 by De Kus - published under The MIT License
Love hurts, love strengthens...
d
darton
Op
Posts: 155
Joined: Sat Jan 21, 2006 11:03 am

Post by darton »

EDIT: Hey it works, you are great De Kus. Thank you very much.
Last edited by darton on Sun May 07, 2006 7:03 am, edited 5 times in total.
User avatar
De Kus
Revered One
Posts: 1361
Joined: Sun Dec 15, 2002 11:41 am
Location: Germany

Post by De Kus »

darton wrote:But it doesn't.
else? and make sure you used a copy from the last edit :P.
De Kus
StarZ|De_Kus, De_Kus or DeKus on IRC
Copyright © 2005-2009 by De Kus - published under The MIT License
Love hurts, love strengthens...
Post Reply