PtokaX forum

Archive => Archived 4.0 boards => Help with Lua 4 scripts => Topic started by: suyang99 on 08 January, 2004, 11:32:45

Title: help with array
Post by: suyang99 on 08 January, 2004, 11:32:45
when i input: !act thr 13 test
then found syntax error: bad argument #1 to 'gsub' (string expected, got nil)

bot = "adsdesign ?? DC++"
sigArr = {}
secArr = {}
thrArr = {}

function Main()
frmHub:GetMinShare()
frmHub:GetCurrentShareAmount()
frmHub:GetHubName()
frmHub:GetUsersCount()
MakeSig()
MakeSec()
MakeThr()
end

function DataArrival(user,data)
   if( strsub(data, 1, 1) == "<" ) then
      data=strsub(data,1,strlen(data)-1)
      _,_,cmd = strfind(data,"%b<>%s+(%S+)")
      _,_,arg = strfind(data,"%b<>%s+%S+%s+(%S+)")
      _,_,arg2 = strfind(data,"%b<>%s+%S+%s+%S+%s+(%S+)")  
      _,_,arg3 = strfind(data,"%b<>%s+%S+%s+%S+%s+%S+%s(%S+)")
      --_,_,arg4 = strfind(data,"%b<>%s+%S+%s+%S+%s+%S+%s+%S+%s(.+)")
      profile = GetProfileName(user.iProfile)
      curuser = user.sName

      if (cmd=="!act") then
         if (profile=="Guest") then
            user:SendData(bot, "?????????รป???????????????!")
            return 1
         else
            if (arg=="sig") then
               if (arg2~=nil) then
                  user:SendData("*"..curuser.." "..sigArr[tonumber(arg2)])
               else
                  user:SendToNick(bot, "???????????, !act ")
               end
            elseif (arg=="sec") then
               if (arg2~=nil) then
                  if (arg3~=nil) then
                     nk = arg3
                  else
                     nk = "??????"
                  end
                  arrTmp = tokenize(secArr[tonumber(arg2)],"#")
                  user:SendData("*"..curuser.." "..arrTmp[1].." "..nk.." "..arrTmp[2])
               else
                  user:SendToNick(bot, "???????????, !act ")
               end
            elseif (arg=="thr") then
               if (arg2~=nil) then
                  if (arg3~=nil) then
                     nk = arg3
                  else
                     nk = "??????"
                  end
                  arrTmp = tokenize(thrArr[tonumber(arg2)],"#")
                  user:SendData("*"..curuser.." "..arrTmp[1].." "..nk.." "..arrTmp[2].." "..nk.." "..arrTmp[3])
               else
                  user:SendToNick(bot, "???????????, !act ")
               end
            elseif (arg=="help") then
               fFileHandle, sError = readfrom("acthelp.txt")
               local sLine = read()
               local sFileContent = ""
               if sLine then
                  sFileContent = sLine
                  while 1 do
                     sLine = read()
                     if not sLine then
                        break
                     else
                        sFileContent = sFileContent..sLine.."\r\n"
                     end
                  end
                  readfrom()   -- close filehandle
                  user:SendPM(bot,"\r\n\r\n"..sFileContent)
                  return 1
               else
                  user:SendPM(bot,"\r\n\r\n".."Nothing found in|")
               end
               readfrom()
               return 1
            end
            return 1
         end
         return 1
      end
   end
end

function tokenize (inString,token)
   _WORDS = {}
   local matcher = "([^"..token.."]+)"
   gsub(inString, matcher, function (w) tinsert(_WORDS,w) end)
   return _WORDS
end

function MakeSig()

   local handle = openfile("sig.dat", "r")
   if (handle~=nil) then
      local line = read(handle)
      while line do
         if (line ~= nil) then
            tinsert(sigArr, line)
         end
         line = read(handle)
      end
      closefile(handle)
   end

end

function MakeSec()

   local handle = openfile("sec.dat", "r")
   if (handle~=nil) then
      local line = read(handle)
      while line do
         if (line ~= nil) then
            tinsert(secArr, line)
         end
         line = read(handle)
      end
      closefile(handle)
   end

end

function MakeThr()

   local handle = openfile("thr.dat", "r")
   if (handle~=nil) then
      local line = read(handle)
      while line do
         if (line ~= nil) then
            tinsert(thrArr, line)
         end
         line = read(handle)
      end
      closefile(handle)
   end

end
Title:
Post by: plop on 08 January, 2004, 13:12:49
before you do: arrTmp = tokenize(secArr[tonumber(arg2)],"#") check if arg2 is actualy in the array and is a number.
this should solve it.
if secArr[tonumber(arg2)] then
   arrTmp = tokenize(secArr[tonumber(arg2)],"#")
else
   user:SendData(bot, arg2.." is a wrong arguments")
end
if arg2 is not in the array or a number gsub get a nil and that error shows up.
same stuff on the thrArr.

plop