Bomb Bloke, on 19 June 2015 - 02:51 AM, said:
Well, let's say the user types in "service stop test" - we want to examine that one word at a time, so we might use string.gmatch to iterate through the words and dump them into separate indexes of a table:
local command = "service stop test"
local words = {}
for word in command:gmatch("%S+") do words[#words + 1] = word end
-- words[1] == "service"
-- words[2] == "stop"
-- words[3] == "test"
Now we can use that new table to help us iterate through your
original table:
local commands = {
[ "service" ] = {
[ "stop" ] = function(...)
print(...)
end,
},
}
local curIndex = commands -- Set curIndex to the pointer to the "commands" table.
for i = 1, #words do
if type(curIndex[words[i]]) == "table" then
curIndex = curIndex[words[i]] -- Shift curIndex to point to the sub-table we found.
elseif type(curIndex[words[i]]) == "function" then
curIndex[words[i]](unpack(words, i + 1)) -- Run the function, supplying the remaining words as arguments.
break
else error("Word not in commands table!") end
end
You are the bomb (pun intended)
I had something along the lines of what you had but I didn't shift while it was looking through it.
Thank you so much!!!!
Edit: So I overcame that problem, now I need to combine files that have tables in them as so:
file1:
[ "test" ] = {
[ "command" ] = function()
print("Test command good")
end,
}
file2:
[ "hey" ] = {
[ "there" ] = function()
print("hey there partner!")
end,
}
final result:
{
[ "test" ] = {
[ "command" ] = function()
print("Test command good")
end,
},
[ "hey" ] = {
[ "there" ] = function()
print("hey there partner!")
end,
},
}
Right now I have this code:
local cDir = fs.combine(dir, 'test')
local commandList = fs.list(cDir)
for k, v in pairs(commandList) do
local h = fs.open(fs.combine(cDir, v), 'r')
local contents = h.readAll()
h.close()
commands[v] = contents
end
but this wouldn't work if the file name was changed.
Edited by jubba890, 19 June 2015 - 03:33 AM.