PtokaX forum

Development Section => Your Developing Problems => Topic started by: kepp on 30 June, 2004, 19:03:21

Title: Problem comparing item in table with string
Post by: kepp on 30 June, 2004, 19:03:21
I would like some help on this, I have a file called SearchFilter.lst

'One word per line'

i load each line into a table, then i want to compare it with another string..

My Main() function

function Main()
   Count = 0
   local handle = readfrom(strPath.."SearchFilter.lst")
   while Count do
      local line = read()
      if line ~= nil then
         tinsert(tblSearch,Count,strlower(line))
      else
         break
      end
   end
   closefile(handle)
end

And this is were i want it to compare and trigger if true

  ["$Search"] =
   
   function(curUser,strData)
      local s,e,strSearchString = strfind(strData,"%?%w%?%d*%?%d*%?(%S+)$")
      if strfind(strSearchString,"$") then
         strSearchString = gsub(strSearchString,"%$"," ")
      end
      -- Is the filter enabled?
      if intEnableSearchFilter == 1 then
         for i,v in tblSearch do
            if v == strSearchString then
               SendToAll("True")
               curUser:SendData(strBOT,"Your search was filtered and found "..strSearchString..", You are now being banned.")
               curUser:TimeBan(20)
               rc = 1;
            end
         end
      end
   end,

I never get the message "True"
rc = return 1
if i do print v however, the data is in the table / Array
Title:
Post by: NotRabidWombat on 30 June, 2004, 20:20:06
Suggested change:
function Main()
   local handle = readfrom(strPath.."SearchFilter.lst")
   local line = read();
   while line do
      tinsert(tblSearch, strlower(line));
      -- more efficient to insert at the end of an array/table
      -- than at the beginning
      line = read();
   end
   closefile(handle)
end
Probably the solution to your problem:
  ["$Search"] =

   function(curUser,strData)
      local s,e,strSearchString = strfind(strData,"%?%w%?%d*%?%d*%?(%S+)$")
      if strfind(strSearchString,"$") then
         strSearchString = gsub(strSearchString,"%$"," ")
      end

      -- make the search string lower case for comparison
      strSearchString = strlower(strSearchString);

      -- Is the filter enabled?
      if intEnableSearchFilter == 1 then
         for i,v in tblSearch do
            if v == strSearchString then
               SendToAll("True")
               curUser:SendData(strBOT,"Your search was filtered and found "..strSearchString..", You are now being banned.")
               curUser:TimeBan(20)
               rc = 1;
            end
         end
      end
   end,
My _guess_ is that strfind will be faster than the for loop method you have right now. So you could use a deliminator charcter, like $, and make one larger string of all filters.

Example:
strFilter = "$porn$something bad$dawson's creek$";

Your search algorithm would be:
strSearchString = "$"..strlower(strSearchString) .. "$";
if( strfind( strFilter, strSearchString ) ) ...

-NotRabidWombat
Title:
Post by: kepp on 30 June, 2004, 21:12:58
Thanks alot for help..

I have 1 question while im  on it
How come i can't access an item as
if MyTable[Word] then
   --// Perform action
end

or is there some rule i must know.. :-/
Title:
Post by: NotRabidWombat on 30 June, 2004, 22:20:06
function Main()
   local handle = readfrom(strPath.."SearchFilter.lst")
   local line = read();
   while line do
      tblSearch[strlower(line)] = 1;
      -- more efficient to insert at the end of an array/table
      -- than at the beginning
      line = read();
   end
   closefile(handle)
end
You were treating the table like an array:
Index {1,2,3...} -> Val
You can treat a table like a hashtable as well:
Key { anything } -> Val
And a hash lookup should perform in constant time, so this may be the best method.

-NotRabidWombat
Title:
Post by: kepp on 30 June, 2004, 23:03:13
ok, i've tried that a couple of times

i can retrieve it like this
tblSearch["porn"]

but not with this
strBadWord = "porn"

tblSearch[strBadWord]
Title:
Post by: kepp on 30 June, 2004, 23:47:55
nevermind...

i added the word between quotes and printed
result bacame

"win98se$r26
"

so regexp was not correct!!
Thanks for your help!!