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.

check from .txt file?

Help for those learning Tcl or writing their own scripts.
Post Reply
R
Regex
Voice
Posts: 19
Joined: Sat Mar 19, 2011 1:23 pm

check from .txt file?

Post by Regex »

Code: Select all

bind pub - !check check
proc check {nick uhost hand chan text} {
  set t [open scripts/filename.txt r]
  set line [gets $t]
  if {[string match -nocase "*$text*" $line]} {
    	putquick "privmsg $chan $text is in the file!"
    	close $t
    } else {
    putquick "privmsg $chan $text isn't in the file!"
    	close $t
  }
}
Isn't workin', how can we check a text if it is in the file? Like that,

<ZAL> !check Is it a problem?
<Egg> Is it a problem? < is in the file !
<ZAL> !check Is THAT a problem?
<Egg> Is THAT a problem? < is not in the file !

Thx.
User avatar
Madalin
Master
Posts: 310
Joined: Fri Jun 24, 2005 11:36 am
Location: Constanta, Romania
Contact:

Post by Madalin »

Try this

Code: Select all

bind pub - !check check

proc check {nick uhost hand chan arg} {
	global found

	unset -nocomplain found 0

	set what [join [lrange $arg 0 end]]

	set fd [open filename r]
	set cont [read $fd]
	close $fd

	foreach line [split $cont "\n"] {
		if {[string match -nocase "*$what*" $line]} {
			putserv "PRIVMSG $chan :'$what' is in the file!"

			set found 1
		}
	}

	if {![info exists found]} {
		putserv "PRIVMSG $chan :Text is not found.."
	}
}
User avatar
SpiKe^^
Owner
Posts: 831
Joined: Fri May 12, 2006 10:20 pm
Location: Tennessee, USA
Contact:

Post by SpiKe^^ »

There's no good reason to make the local process variable 'found' into a global variable.
It's never reused and forces us to unbind it to run the proc again.

Also 'arg' is not a list and shouldn't be handled like this... [join [lrange $arg 0 end]]

I might suggest something more like...

Code: Select all

bind pub - !check check 

proc check {nick uhost hand chan arg} { 

   set fd [open scripts/filename.txt r] 
   set cont [read $fd] 
   close $fd 

   foreach line [split $cont "\n"] { 
      if {[string match -nocase "*$arg*" $line]} { 
         putserv "PRIVMSG $chan :'$arg' is in the file!" 

         set found 1  ;  break
      } 
   } 

   if {![info exists found]} { 
      putserv "PRIVMSG $chan :Text is not found.." 
   } 
} 
 
SpiKe^^

Get BogusTrivia 2.06.4.7 at www.mytclscripts.com
or visit the New Tcl Acrhive at www.tclarchive.org
.
User avatar
Madalin
Master
Posts: 310
Joined: Fri Jun 24, 2005 11:36 am
Location: Constanta, Romania
Contact:

Post by Madalin »

Well i make the code as i know :) if i encouter problems in the future with codes i write then i modify the code.. but any modifications/improvment of my code is welcomed :) im always learning.. i also never used 'break'
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

Never trust user input, especially when it's not user restricted. How about this?

Code: Select all

bind pub - !check check

proc check {nick uhost hand chan text} {
	set what [join [lrange $text 0 end]]
	set fo [open "scripts/filename.txt" r]
	set data [split [read $fo] "\n"]
	close $fo
	if {[lsearch -glob $data $what]} {
		puthelp "PRIVMSG $chan :'$what' is in the file!"
	} else {
		puthelp "PRIVMSG $chan :Text is not found.."
   }
}
PS: supports wildcards.
Once the game is over, the king and the pawn go back in the same box.
Post Reply