proc fishbot {nick uhost handle chan text} {
if {![string match "*+fish*" [channel info $chan]]} { return 0 }
set fish1 "you can't just pick people at random!"
set fish2 "wertle"
if {[string match "$fish1" $text]} { putserv "PRIVMSG $chan : I can do anything I like, $nick, I'm eccentric! Rrarrrrrgh! Go!" }
if {[string match "$fish2" $text]} { putserv "PRIVMSG $chan : moo" }
}
the list goes on that way.. fish3, fish4... and the responses do aswell.
what's the best way to solve this? with 2 arrays? (questions <--> answers)
set crap {
"question" "answer"
"blah*" "$nick is verbose!"
"* bleh *" "no."
"bluh" "i hate $chan"
}
foreach {m a} $crap {
if {[string match $m $text]} {
# the 'subst' takes care of variable/command/backslash substitution
# in the string contained in 'a'.
putserv "PRIVMSG $chan :[subst $a]"
# then we 'break' to avoid answering more than once to each line
# of text and also cut down on the cpu usage
break
}
}
You could of course store the mask/reply pairs in an array and loop through 'array names' or 'array get' instead.
Also make sure you put the most commonly matched masks first.
If you plan to have thousands of elements the best thing would probably be to sort them some way and use a binary search approach to decrease the number of mask having to be matched.
Last edited by user on Wed Jun 11, 2003 8:59 am, edited 1 time in total.
# outside the proc:
# array of masks/answers
array set fishA {
"question" "answer"
"blah*" "$nick is verbose!"
"* bleh *" "no."
"bluh" "i hate $chan"
}
# array containing lists of masks starting with a certain char
array unset fishM
foreach e [array names fishA] {
lappend fishM([string index $e 0]) $e
}
# and a sorted list of the chars
set fishC [lsort -dict [array names fishM]]
proc fishbot {nick uhost handle chan text} {
# converting $text to lowercase and making sure all your masks also are
# in lowercase might be a good idea to avoid multiple case conversions
global fishA fishM fishC
# first we make a list of masks that might match $text
# (based on the first char)
set masks {}
foreach e $fishC {
if {[string match $e* $text]} {
set masks [concat $masks $fishM($e)]
}
}
# then we do the actual matching
foreach m $masks {
if {[string match $m $text]} {
putserv "PRIVMSG $chan :[subst $fishA($m)]"
break
}
}
}