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

Post by darton »

Now I have a last question. Is this possible: If someone in the channel writes "!medals" my bot looks for his name in the textfile and says for example:"$nick has 2 MVP Medal(s) and 1 MostKills Medal(s)". If the bot could not find the name in the textfile it should say: "$nick has no medals yet."
User avatar
De Kus
Revered One
Posts: 1361
Joined: Sun Dec 15, 2002 11:41 am
Location: Germany

Post by De Kus »

you can creat a modified copy of the above script. [lindex $line 1] and [lindex $line 2] will contain the counter of the medals and if lsearch returns -1, there is no record of medals of him.
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 »

Code: Select all

set medalsfile "scripts/medals.txt"

bind pub - !medals medals:get
proc medals:get {nick uhost hand chan arg} {
  set fd [open $::medalsfile r+]
      while {![eof $fd]} {
         lappend list [gets $fd]
      }
  if {[set le [lsearch -glob $list "$nick *"]] != -1} { 
       set line [lindex $list $le]
       set mvp [lindex $line 1]
       set kills [lindex $line 2]
     putquick "PRIVMSG $chan :$nick has $mvp MVP Medal(s) and $kills MostKills Medal(s)."   
  } else {
       putquick "PRIVMSG $chan :$nick has no medals yet." 
   }
}
I made that script. It works. I hope its a "proper syntax". :wink:
Thanks again for your work De Kus.
User avatar
De Kus
Revered One
Posts: 1361
Joined: Sun Dec 15, 2002 11:41 am
Location: Germany

Post by De Kus »

well, I would have used a 'return 1' at the end, used puthelp instead of putquick and changed the r+ to r, since we nolonger need to open the file in read and write modus :D, but its exactly that what I thought you should make :).

only 1 note: you forgot to close the file :).
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 »

Does it make any difference where I put in the close command? So this should be right?

Code: Select all

set medalsfile "scripts/medals.txt"

bind pub - !medals medals:get
proc medals:get {nick uhost hand chan arg} {
  set fd [open $::medalsfile r+]
      while {![eof $fd]} {
         lappend list [gets $fd]
      }
  if {[set le [lsearch -glob $list "$nick *"]] != -1} {
       set line [lindex $list $le]
       set mvp [lindex $line 1]
       set kills [lindex $line 2]
     putquick "PRIVMSG $chan :$nick has $mvp MVP Medal(s) and $kills MostKills Medal(s)."   
  } else {
       putquick "PRIVMSG $chan :$nick has no medals yet."
   }
   close $fd 
}
d
darton
Op
Posts: 155
Joined: Sat Jan 21, 2006 11:03 am

Post by darton »

The bot seems to have problems with this nick: {{As}masterblast{GEN}}
It does not make an own line for each time, but it makes a new line everytime for this nick, when it has MVP or MostKills.
Does anyone know why?
User avatar
De Kus
Revered One
Posts: 1361
Joined: Sun Dec 15, 2002 11:41 am
Location: Germany

Post by De Kus »

does the nick stand in the text file exactly like that, or are the {} excaped with \?
Maybe lsearch doenst find it, because of the sublist. You could try to use

Code: Select all

lappend list "$name $mvp $kills"
instead for a pure string.
You must remove escape characters from the current textfile, of course.
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 »

Yes in the textfile, there is exactly that nick. When I type "!medals {{As}masterblast{GEN}}" lsearch find this nick. But when I want to store this nick a new line is made everytime. What do you mean with

Code: Select all

lappend list "$name $mvp $kills"
Instead of what do I have to use this?
User avatar
De Kus
Revered One
Posts: 1361
Joined: Sun Dec 15, 2002 11:41 am
Location: Germany

Post by De Kus »

Start using your brain man, there is only line that uses similar vars with lappend. But strange that he finds the nick and creates a new entry anyway... thats irrationel :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...
Post Reply