| View previous topic :: View next topic |
| Author |
Message |
Regex Voice
Joined: 19 Mar 2011 Posts: 19
|
Posted: Mon Apr 23, 2012 9:25 am Post subject: check from .txt file? |
|
|
| Code: |
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. |
|
| Back to top |
|
 |
Madalin Master

Joined: 24 Jun 2005 Posts: 310 Location: Constanta, Romania
|
Posted: Tue Jan 29, 2013 3:04 pm Post subject: |
|
|
Try this
| Code: |
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.."
}
}
|
_________________ https://github.com/MadaliNTCL - To chat with me: https://tawk.to/MadaliNTCL |
|
| Back to top |
|
 |
SpiKe^^ Owner

Joined: 12 May 2006 Posts: 792 Location: Tennessee, USA
|
Posted: Tue Jan 29, 2013 4:55 pm Post subject: |
|
|
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: |
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
. |
|
| Back to top |
|
 |
Madalin Master

Joined: 24 Jun 2005 Posts: 310 Location: Constanta, Romania
|
Posted: Tue Jan 29, 2013 4:59 pm Post subject: |
|
|
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' _________________ https://github.com/MadaliNTCL - To chat with me: https://tawk.to/MadaliNTCL |
|
| Back to top |
|
 |
caesar Mint Rubber

Joined: 14 Oct 2001 Posts: 3741 Location: Mint Factory
|
Posted: Wed Jan 30, 2013 7:19 am Post subject: |
|
|
Never trust user input, especially when it's not user restricted. How about this?
| Code: |
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. |
|
| Back to top |
|
 |
|