OP stated he had hundred of words to look for and Sakana suggested how a while loop would be a disaster.
Yet you come up with a double nested while loop even though the OP already had a code with only one loop (the second nested loop being handled by $read itself)
But your code is even more inefficient, you use the same $read twice and you don't use $read to do the second loop. Your $read also don't have the 'n' switch, which will evaluate the line, resulting in possible exploits.
@FRAG_B: A more efficient way is to avoid using while loop, you can keep the list of words in the text file but you should convert this text file to one long string, representing a regular expression, to be used like Sakana suggested:
on *:start:filetovar
alias add_word {
if (!$read(listofwords.txt,tnw,$1)) {
write listofwords.txt $1
filetovar
}
}
alias del_word {
if ($read(listofwords.txt,tnw,$1)) {
write -dl $readn listofwords.txt
filetovar
}
}
alias list_word loadbuf -a listofwords.txt
alias filetovar {
set %words_reg /\b(
var %a 1
while ($read(listofwords.txt,tn,%a) != $null) {
set %words_reg $+(%words_reg,\Q,$replacecs($v1,\E,\E\\E\Q),\E|)
inc %a
}
set %words_reg $left(%words_reg,-1) $+ )\b/Si
}
on $*:text:%words_reg:#chan:msg $chan You matched the word $regml(1) in my file!
We're still using a loop to convert the file to a variable, which is slow, but that's done when adding/deleting, not when the on text event trigger and that's where we need to be fast. Of course that while loop could also be improved but that's besides the point here. Use /add_word <word> to add a word, /del_word <word> to delete a word, and /list_word to display the list into the active window. If you edit the text file manually, you can simply execute /filetovar to update the variable containing the regular expression
Note that this method has one problem, it is limited in the number of words you can have in the text file.
If you reach the limit (you'll get a /set: line too long error) there are others way to make it better.