Author Topic: Table structures  (Read 4107 times)

0 Members and 1 Guest are viewing this topic.

Markitos

  • Guest
Table structures
« on: 30 March, 2006, 10:53:29 »
This is a table structure that i was trying to write instead of repeating many elseif...it's FaqBot script writen by TTB
Code: [Select]
function ChatArrival(curUser,data)
HowToSend = "Main"
data = string.sub(data,1,string.len(data)-1)
local s,e,cmd = string.find(data,"^%b<>%s+[%!%+%#%?](%S+)")
local _,_,_,fileordir = string.find(data,"^%b<>%s+(%S+)%s+([^|^ ]+)")
if cmd then
local tCmds = {
["faqsave"] = function(curUser,data)
if Profiles[curUser.iProfile] == 1 then
FaqSave(curUser,data,fileordir)
return 1
end,

["faq"] = function(curUser,data)
FaqLoad(curUser,data,fileordir)
return 1
end,
["faqdel"] = function(curUser,data)
if Profiles[curUser.iProfile] == 1 then
FaqDel(curUser,data,fileordir)
return 1
end,
["sfaq"] = function(curUser,data)
if Profiles[curUser.iProfile] == 1 then
FaqSend(curUser,data,fileordir)
return 1
end,
["faqrefresh"] = function(curUser,data)
if Profiles[curUser.iProfile] == 1 then
FaqRefresh(curUser,data)
return 1
end,
["faqhelp"] = function (curUser,data)
if Profiles[curUser.iProfile] == 1 then
SendToUser(curUser, "\r\n\r\n"..
"\t--<>--------------------------------------------------------------------------------------------------------------------------------------------------------<>--\r\n"..
"\t\t\t [ FAQ Help ]\t\t\t [ FAQ Help ] \r\n"..
"\t--<>--------------------------------------------------------------------------------------------------------------------------------------------------------<>--\r\n"..
"\t\t"..prefix.."faq \t\t\t=\tShows available faqs\r\n"..
"\t\t"..prefix.."faq <faqname>\t\t=\tLoad a faq\r\n"..
"\t\t"..prefix.."sfaq <faqname> <nick> \t=\tSend faq to nick (only for OP's)\r\n"..
"\t\t"..prefix.."faqsave <faqname> <info> \t=\tSave to file (only for OP's)\r\n"..
"\t\t"..prefix.."faqdel <faqname>\t\t=\tDelete faq (only for OP's)\r\n"..
"\t\t"..prefix.."faqrefresh \t\t=\tRefresh faq-index (only for OP's)\r\n"..
"\t\t"..prefix.."faqhelp \t\t\t=\tShow help (only for OP's)\r\n"..
"\t--<>--------------------------------------------------------------------------------------------------------------------------------------------------------<>--\r\n")
return 1
end
}
if tCmds[cmd] then
return tCmds[cmd](user,data),1
end
end
but i get this error FaqBot_LUA5.lua:44: unexpected symbol near '['
help woul be appreaciated
« Last Edit: 30 March, 2006, 10:56:33 by Markitos »

PtokaX forum

Table structures
« on: 30 March, 2006, 10:53:29 »

Herodes

  • Guest
Re: Table structures
« Reply #1 on: 30 March, 2006, 13:20:48 »
Try the following... some 'end's where missing .. :)
Code: [Select]
function ChatArrival(curUser,data)
HowToSend = "Main"
data = string.sub(data,1,string.len(data)-1)
local s,e,cmd = string.find(data,"^%b<>%s+[%!%+%#%?](%S+)")
local _,_,_,fileordir = string.find(data,"^%b<>%s+(%S+)%s+([^|^ ]+)")
if cmd then
local tCmds = {
["faqsave"] = function(curUser,data)
if Profiles[curUser.iProfile] == 1 then
FaqSave(curUser,data,fileordir)
end
return 1
end,

["faq"] = function(curUser,data)
FaqLoad(curUser,data,fileordir)
return 1
end,
["faqdel"] = function(curUser,data)
if Profiles[curUser.iProfile] == 1 then
FaqDel(curUser,data,fileordir)
end
return 1
end,
["sfaq"] = function(curUser,data)
if Profiles[curUser.iProfile] == 1 then
FaqSend(curUser,data,fileordir)
end
return 1
end,
["faqrefresh"] = function(curUser,data)
if Profiles[curUser.iProfile] == 1 then
FaqRefresh(curUser,data)
end
return 1
end,
["faqhelp"] = function (curUser,data)
if Profiles[curUser.iProfile] == 1 then
SendToUser(curUser, "\r\n\r\n"..
"\t--<>--------------------------------------------------------------------------------------------------------------------------------------------------------<>--\r\n"..
"\t\t\t [ FAQ Help ]\t\t\t [ FAQ Help ] \r\n"..
"\t--<>--------------------------------------------------------------------------------------------------------------------------------------------------------<>--\r\n"..
"\t\t"..prefix.."faq \t\t\t=\tShows available faqs\r\n"..
"\t\t"..prefix.."faq <faqname>\t\t=\tLoad a faq\r\n"..
"\t\t"..prefix.."sfaq <faqname> <nick> \t=\tSend faq to nick (only for OP's)\r\n"..
"\t\t"..prefix.."faqsave <faqname> <info> \t=\tSave to file (only for OP's)\r\n"..
"\t\t"..prefix.."faqdel <faqname>\t\t=\tDelete faq (only for OP's)\r\n"..
"\t\t"..prefix.."faqrefresh \t\t=\tRefresh faq-index (only for OP's)\r\n"..
"\t\t"..prefix.."faqhelp \t\t\t=\tShow help (only for OP's)\r\n"..
"\t--<>--------------------------------------------------------------------------------------------------------------------------------------------------------<>--\r\n")
end
return 1
end
}
if tCmds[cmd] then
return tCmds[cmd](user,data),1
end
end

Offline jiten

  • Scripter
  • Forum Legend
  • ******
  • Posts: 1 577
  • Karma: +71/-5
Re: Table structures
« Reply #2 on: 30 March, 2006, 13:42:25 »
Your code is missing some "end"s. Note that every "if" should end with an "end".
By the way, if you're creating a local command table inside ChatArrival, there's no need to use: function(curUser,data) again. That would be needed if it was created outside.
You could also remove the "return 1"s inside each command, as there will be duplication with this:
Code: [Select]
return tCmds[cmd](user,data),1Correct tabbing would be helpful too. Well, hope this helps some :)

Regards

Markitos

  • Guest
Re: Table structures
« Reply #3 on: 30 March, 2006, 19:00:20 »
thanks 4 ur replies, i tried to fixe it but i got a new error \Ptokax\scripts\FaqBot_LUA5.lua:38: '=' expected near 'if'
Code: [Select]
function ChatArrival(curUser,data)
HowToSend = "Main"
data = string.sub(data,1,string.len(data)-1)
local s,e,cmd = string.find(data,"^%b<>%s+[%!%+%#%?](%S+)")
local _,_,_,fileordir = string.find(data,"^%b<>%s+(%S+)%s+([^|^ ]+)")
if cmd then
local tCmds = {
["faqsave"]
if Profiles[curUser.iProfile] == 1 then
FaqSave(curUser,data,fileordir)
end
end,

["faq"]
FaqLoad(curUser,data,fileordir)
end,
["faqdel"]
if Profiles[curUser.iProfile] == 1 then
FaqDel(curUser,data,fileordir)
end
end,
["sfaq"]
if Profiles[curUser.iProfile] == 1 then
FaqSend(curUser,data,fileordir)
end
end,
["faqrefresh"]
if Profiles[curUser.iProfile] == 1 then
FaqRefresh(curUser,data)
end
end,
["faqhelp"]
if Profiles[curUser.iProfile] == 1 then
SendToUser(curUser, "\r\n\r\n"..
"\t--<>--------------------------------------------------------------------------------------------------------------------------------------------------------<>--\r\n"..
"\t\t\t [ FAQ Help ]\t\t\t [ FAQ Help ] \r\n"..
"\t--<>--------------------------------------------------------------------------------------------------------------------------------------------------------<>--\r\n"..
"\t\t"..prefix.."faq \t\t\t=\tShows available faqs\r\n"..
"\t\t"..prefix.."faq <faqname>\t\t=\tLoad a faq\r\n"..
"\t\t"..prefix.."sfaq <faqname> <nick> \t=\tSend faq to nick (only for OP's)\r\n"..
"\t\t"..prefix.."faqsave <faqname> <info> \t=\tSave to file (only for OP's)\r\n"..
"\t\t"..prefix.."faqdel <faqname>\t\t=\tDelete faq (only for OP's)\r\n"..
"\t\t"..prefix.."faqrefresh \t\t=\tRefresh faq-index (only for OP's)\r\n"..
"\t\t"..prefix.."faqhelp \t\t\t=\tShow help (only for OP's)\r\n"..
"\t--<>--------------------------------------------------------------------------------------------------------------------------------------------------------<>--\r\n")
end
end,
}
if tCmds[cmd] then
return tCmds[cmd](curUser,data),1
end
end
« Last Edit: 30 March, 2006, 20:25:26 by Markitos »

Herodes

  • Guest
Re: Table structures
« Reply #4 on: 30 March, 2006, 19:38:37 »
Noted below :/
Code: [Select]
--- compare this
["faqsave"] = function( curUser, data )
if Profiles[curUser.iProfile] == 1 then
FaqSave(curUser,data,fileordir)
end
end,
---- to this
["faq"]
FaqLoad(curUser,data,fileordir)
end,

Markitos

  • Guest
Re: Table structures
« Reply #5 on: 30 March, 2006, 20:22:17 »
Noted below :/
Code: [Select]
--- compare this
["faqsave"] = function( curUser, data )
if Profiles[curUser.iProfile] == 1 then
FaqSave(curUser,data,fileordir)
end
end,
---- to this
["faq"]
FaqLoad(curUser,data,fileordir)
end,
Ops...edited but still same error  :-[
« Last Edit: 30 March, 2006, 20:32:47 by Markitos »

Offline Madman

  • Scripter
  • Emperor
  • ******
  • Posts: 1 316
  • Karma: +75/-1
Re: Table structures
« Reply #6 on: 30 March, 2006, 20:52:38 »
You are missing an end at the end.
You don't close the if cmd then....
Tab the commands and the
Code: [Select]
if tCmds[cmd] then
return tCmds[cmd](curUser,data),1
end
and you will see it
We suffer in silence, we lurk in the shadows, we kill in the night
Site currently down, ETA of returning online is 2099 ;p

Markitos

  • Guest
Re: Table structures
« Reply #7 on: 30 March, 2006, 21:02:10 »
You are missing an end at the end.
You don't close the if cmd then....
Tab the commands and the
Code: [Select]
if tCmds[cmd] then
return tCmds[cmd](curUser,data),1
end
and you will see it
still  ??? ??? ???

Offline bastya_elvtars

  • Forum God
  • ****
  • Posts: 3 744
  • Karma: +173/-7
  • The rock n' roll doctor
    • The FreshStuff3 Site
Re: Table structures
« Reply #8 on: 30 March, 2006, 23:09:09 »
If you have variable numbers of args, you can store them in an array and call the function unpack on it. Like: f(unpack({user,data}))
Everything could have been anything else and it would have just as much meaning.

Markitos

  • Guest
Re: Table structures
« Reply #9 on: 31 March, 2006, 08:14:54 »
thanks for your replies but now its another error  scripts\FaqBot_LUA5.lua:79: attempt to index global 'tCmds' (a nil value)
The whole script...
Code: [Select]
-- FAQ bot by TTB
-- Stripped from [BOT]Vliegenmepper
-- Build on 28-10-2005
---------------------------------------------------------------------------------------------------
--- Settings
---------------------------------------------------------------------------------------------------
bot = "[BOT]Faq"
prefix = "!"
-->> Who can add // delete // send faqs?
Profiles = {
[-1] = 0,  -- Users
[0] = 1,   -- Masters
[1] = 1,   -- OPs
[2] = 0,   -- VIPs
[3] = 0,   -- REGs
[4] = 1,   -- MODs
[5] = 1,   -- Founders
}

---------------------------------------------------------------------------------------------------
--- Standard functions
---------------------------------------------------------------------------------------------------
indexfile = "faqindex" -- already has *.txt extension
faqfolder = "faqbot/"

function Main()
frmHub:RegBot(bot)
end

function ChatArrival(curUser,data)
HowToSend = "Main"
data = string.sub(data,1,string.len(data)-1)
local s,e,cmd = string.find(data,"^%b<>%s(%p)(%S+)")
local _,_,_,fileordir = string.find(data,"^%b<>%s+(%S+)%s+([^|^ ]+)")
if cmd then
local tCmds = {
["faqsave"] = function()
if Profiles[curUser.iProfile] == 1 then
FaqSave(curUser,data,fileordir)
end
end,

["faq"] = function()
FaqLoad(curUser,data,fileordir)
end,
["faqdel"] = function()
if Profiles[curUser.iProfile] == 1 then
FaqDel(curUser,data,fileordir)
end
end,
["sfaq"] = function()
if Profiles[curUser.iProfile] == 1 then
FaqSend(curUser,data,fileordir)
end
end,
["faqrefresh"] = function()
if Profiles[curUser.iProfile] == 1 then
FaqRefresh(curUser,data)
end
end,
["faqhelp"] = function()
if Profiles[curUser.iProfile] == 1 then
SendToUser(curUser, "\r\n\r\n"..
"\t--<>"..string.rep("-",152).."<>--\r\n"..
"\t\t\t [ FAQ Help ]\t\t\t [ FAQ Help ] \r\n"..
"\t--<>"..string.rep("-",152).."<>--\r\n"..
"\t\t"..prefix.."faq \t\t\t=\tShows available faqs\r\n"..
"\t\t"..prefix.."faq <faqname>\t\t=\tLoad a faq\r\n"..
"\t\t"..prefix.."sfaq <faqname> <nick> \t=\tSend faq to nick (only for OP's)\r\n"..
"\t\t"..prefix.."faqsave <faqname> <info> \t=\tSave to file (only for OP's)\r\n"..
"\t\t"..prefix.."faqdel <faqname>\t\t=\tDelete faq (only for OP's)\r\n"..
"\t\t"..prefix.."faqrefresh \t\t=\tRefresh faq-index (only for OP's)\r\n"..
"\t\t"..prefix.."faqhelp \t\t\t=\tShow help (only for OP's)\r\n"..
"\t--<>"..string.rep("-",152).."<>--\r\n")
end
end,
}
end
if tCmds[cmd] then
return tCmds[cmd](curUser,data),1
end
end

---------------------------------------------------------------------------------------------------
--- Command functions
---------------------------------------------------------------------------------------------------
function giveindex(curUser, indexfile)
local handle = io.input(indexfile..".txt", "r")
local iets = nil
local line = ""
for line in handle:lines() do
if iets then
iets = iets..", "..line
else
iets = line
end
end
if iets == nil then
SendToUser(curUser,"Sorry, I don't have any FAQ in my memory!")
else
SendToUser(curUser, "OK, I'll try to help you! Type: "..prefix.."faq <subject>. Here are the FAQs you can ask me:\r\n->["..iets.."]<-")
end
handle:close()
end

function FaqLoad(curUser,data,fileordir)
if fileordir == nil then
giveindex(curUser,indexfile)
else
local sdata = LoadFile(fileordir)
if sdata == "" then
SendToUser(curUser, "FAQ  '"..fileordir.."'  doesn't exist!|")
else
SendToAll(bot, curUser.sName.." is reading the faq:  '"..fileordir.."'")
curUser:SendPM(bot, "Requested FAQ = "..fileordir..": \r\n"..sdata.."|")
end
end
end

function FaqSave(curUser,data,fileordir)
local s,e,sdata = string.find(data,"^%b<>%s+%S+%s+%S+%s+(.*)")
if fileordir ~= nil and sdata ~= nil then
SaveFile(fileordir,sdata,curUser)
dir = faqfolder
os.execute("dir \""..dir.."\" /o:n /b /l /d > "..indexfile..".txt")
else
  SendToUser(curUser, "Syntax error! Use: "..prefix.."faqsave <faqname> <text> to save your FAQ!|")
end
end

function FaqDel(curUser,data,fileordir)
if fileordir == nil then
SendToUser(curUser, "Syntax error! Use: "..prefix.."faqdel <faqname> to delete a FAQ!")
else
DelFile(fileordir,curUser)
end
end

function FaqSend(curUser,data,fileordir)
local s,e,nick = string.find(data,"^%b<>%s+%S+%s+%S+%s+(.*)")
if nick == nil or fileordir == nil then
SendToUser(curUser, "*** Syntax error! Use: "..prefix.."sfaq <faq> <nick> to send a faq!")
elseif GetItemByName(nick) == nil then
SendToUser(curUser, "*** The user: "..nick.." is NOT online!")
elseif GetItemByName(nick) then
local sdata = LoadFile(fileordir)
if sdata == "" then
  SendToUser(curUser, "Faq  '"..fileordir.."'  doesn't exist!")
else
  SendPmToNick(nick,bot, curUser.sName.." did send you the following faq = "..fileordir..": \r\n"..sdata)
masslevel("The faq ' "..fileordir.." ' has been sent to the user: "..nick..".  Done by: "..curUser.sName,1)
end
end
end

function SendToUser(curUser, message)
if HowToSend == "Main" then
curUser:SendData(bot, message)
end
end

---------------------------------------------------------------------------------------------------
--- File handling
---------------------------------------------------------------------------------------------------
function FaqRefresh(curUser,data)
dir = faqfolder
os.execute("dir \""..dir.."\" /o:n /b /l /d > "..indexfile..".txt")
SendToUser(curUser, "Faq-index refreshed and ready for use!")
end

function DelFile(filename, curUser)
local handle = io.open(faqfolder..filename,"r")
if handle then
handle:flush()
handle:close()
dir = faqfolder
os.remove(dir..filename)
SendToAll(bot, curUser.sName.." deleted the FAQ:  '"..filename.."'")
os.execute("dir \""..dir.."\" /o:n /b /l /d > "..indexfile..".txt")
return 1
else
SendToUser(curUser, "FAQ  '"..filename.."'  doesn't exist and can't be deleted!|")
return 1
end
end

function SaveFile(filename,sdata,curUser)
local handle = io.open (faqfolder..filename,"r")
if handle then
SendToUser(curUser, "Faq already exists! Please delete the file first before saving it, or choose a different faqname!")
else
handle = io.open (faqfolder..filename,"w")
handle:write(sdata.."  ["..curUser.sName.."], "..os.date("%d-%y-20%y").."]")
    SendToAll(bot, curUser.sName.." saved a new FAQ:  '"..filename.."'          Type:    "..prefix.."faq "..filename.."    in the MAIN CHAT to view this new FAQ!")
dir = faqfolder
os.execute("dir \""..dir.."\" /o:n /b /l /d > "..indexfile..".txt")             
end
handle:flush()
handle:close()
return 1
end

function LoadFile(filename,curUser)
local handle = io.open(faqfolder..filename,"r")
if handle then
local sdata = ""
local line = ""
for line in handle:lines() do
sdata = sdata..line.."\r\n"
end
handle:flush()
handle:close()
return sdata
else
sdata = ""
handle:flush()
handle:close()
return sdata
end
end

P.S - Bastya ill check the wiki and your script to see examples...

Herodes

  • Guest
Re: Table structures
« Reply #10 on: 31 March, 2006, 09:01:33 »
How about this??
Code: [Select]
function ChatArrival(curUser,data)
HowToSend = "Main"
data = string.sub(data,1,string.len(data)-1)
local s,e,cmd = string.find(data,"^%b<>%s(%p)(%S+)")
local _,_,_,fileordir = string.find(data,"^%b<>%s+(%S+)%s+([^|^ ]+)")
if cmd then
local tCmds = {
["faqsave"] = function()
if Profiles[curUser.iProfile] == 1 then
FaqSave(curUser,data,fileordir)
end
end,

["faq"] = function()
FaqLoad(curUser,data,fileordir)
end,
["faqdel"] = function()
if Profiles[curUser.iProfile] == 1 then
FaqDel(curUser,data,fileordir)
end
end,
["sfaq"] = function()
if Profiles[curUser.iProfile] == 1 then
FaqSend(curUser,data,fileordir)
end
end,
["faqrefresh"] = function()
if Profiles[curUser.iProfile] == 1 then
FaqRefresh(curUser,data)
end
end,
["faqhelp"] = function()
if Profiles[curUser.iProfile] == 1 then
SendToUser(curUser, "\r\n\r\n"..
"\t--<>"..string.rep("-",152).."<>--\r\n"..
"\t\t\t [ FAQ Help ]\t\t\t [ FAQ Help ] \r\n"..
"\t--<>"..string.rep("-",152).."<>--\r\n"..
"\t\t"..prefix.."faq \t\t\t=\tShows available faqs\r\n"..
"\t\t"..prefix.."faq <faqname>\t\t=\tLoad a faq\r\n"..
"\t\t"..prefix.."sfaq <faqname> <nick> \t=\tSend faq to nick (only for OP's)\r\n"..
"\t\t"..prefix.."faqsave <faqname> <info> \t=\tSave to file (only for OP's)\r\n"..
"\t\t"..prefix.."faqdel <faqname>\t\t=\tDelete faq (only for OP's)\r\n"..
"\t\t"..prefix.."faqrefresh \t\t=\tRefresh faq-index (only for OP's)\r\n"..
"\t\t"..prefix.."faqhelp \t\t\t=\tShow help (only for OP's)\r\n"..
"\t--<>"..string.rep("-",152).."<>--\r\n")
end
end,
}
if tCmds[cmd] then
return tCmds[cmd](curUser,data),1
end
end
end

Markitos

  • Guest
Re: Table structures
« Reply #11 on: 31 March, 2006, 09:09:47 »
still...but now when i type a cmd, ptokax dont report any error

Offline jiten

  • Scripter
  • Forum Legend
  • ******
  • Posts: 1 577
  • Karma: +71/-5
Re: Table structures
« Reply #12 on: 31 March, 2006, 14:00:07 »
I didn't have a thorough look at the code posted by Mutor/Herodes but, this caught my attention:

Code: [Select]
local s,e,cmd = string.find(data,"^%b<>%s(%p)(%S+)")
As you may have noticed, a prefix is being assigned to cmd so it won't really answer to commands.

Try this one:

Code: [Select]
local s,e,cmd = string.find(data,"^%b<>%s%p(%S+)")

Markitos

  • Guest
Re: Table structures
« Reply #13 on: 31 March, 2006, 19:31:53 »
Thanks all...i got it working  ;D

PtokaX forum

Re: Table structures
« Reply #13 on: 31 March, 2006, 19:31:53 »