Problem comparing item in table with string
 

News:

29 December 2022 - PtokaX 0.5.3.0 (20th anniversary edition) released...
11 April 2017 - PtokaX 0.5.2.2 released...
8 April 2015 Anti child and anti pedo pr0n scripts are not allowed anymore on this board!
28 September 2015 - PtokaX 0.5.2.1 for Windows 10 IoT released...
3 September 2015 - PtokaX 0.5.2.1 released...
16 August 2015 - PtokaX 0.5.2.0 released...
1 August 2015 - Crowdfunding for ADC protocol support in PtokaX ended. Clearly nobody want ADC support...
30 June 2015 - PtokaX 0.5.1.0 released...
30 April 2015 Crowdfunding for ADC protocol support in PtokaX
26 April 2015 New support hub!
20 February 2015 - PtokaX 0.5.0.3 released...
13 April 2014 - PtokaX 0.5.0.2 released...
23 March 2014 - PtokaX testing version 0.5.0.1 build 454 is available.
04 March 2014 - PtokaX.org sites were temporary down because of DDOS attacks and issues with hosting service provider.

Main Menu

Problem comparing item in table with string

Started by kepp, 30 June, 2004, 19:03:21

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

kepp

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
Guarding    

NotRabidWombat

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


I like childish behavior. Maybe this post will be deleted next.

kepp

#2
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.. :-/
Guarding    

NotRabidWombat

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


I like childish behavior. Maybe this post will be deleted next.

kepp

#4
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]
Guarding    

kepp

nevermind...

i added the word between quotes and printed
result bacame

"win98se$r26
"

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

SMF spam blocked by CleanTalk