-
Posted by jon on Mon, 27 Feb 2012 19:38:42
What is wrong?
local function handler(msg, editbox) local command, resst, ressst = msg:match("^(%S*)%s*(.-)$"); if command == "add" and resst ~= "" and ressst ~= "" then print("command "..resst.." rest "..ressst.." ttt") elseif command == "remove" and resst ~= "" and ressst ~= "" then else print("Syntax: /yourcmd (add|remove) someIdentifier"); end end
I'm writing /command $add $resst $ressst
-
Posted by jnwhiteh on Mon, 27 Feb 2012 20:03:06
I'm not sure what you're saying at the end, but here are some comments:
ressst will always be nil, because your pattern only has two captures.
The following seems to be what you want, perhaps:
local function handler(msg, editbox) local command, rest = msg:match("^(%S*)%s*(.-)$"); if command == "add" and #rest > 0 then print("Got request to add " .. rest) elseif command == "remove" and #rest > 0 then print("Got request to remove " .. rest) else print("Syntax: /yourcmd (add|remove) someIdentifier"); end end
-
Posted by jon on Mon, 27 Feb 2012 20:12:50
I'm not sure what you're saying at the end, but here are some comments:
ressst will always be nil, because your pattern only has two captures.
The following seems to be what you want, perhaps:
local function handler(msg, editbox) local command, rest = msg:match("^(%S*)%s*(.-)$"); if command == "add" and #rest > 0 then print("Got request to add " .. rest) elseif command == "remove" and #rest > 0 then print("Got request to remove " .. rest) else print("Syntax: /yourcmd (add|remove) someIdentifier"); end end
No I need to 3 values were. For example: /command values1 values2 values3
-
Posted by jnwhiteh on Mon, 27 Feb 2012 20:16:15
Then your pattern is wrong. How about the following:
local function handler(msg, editbox) -- Grab all non-space tokens into a table local tokens = {} for token in msg:gmatch("%S+") do tokens[#tokens+1] = token end local command = tokens[1] if command == "add" and tokens[2] and tokens[3] then print(string.format("Add: %s, %s", tokens[2], tokens[3])) elseif command == "remove" and tokens[2] and tokens[3] then print(string.format("Remove: %s, %s", tokens[2], tokens[3])) else print("Syntax: /yourcmd (add|remove) someIdentifier"); end end
-
Posted by jon on Mon, 27 Feb 2012 20:24:59
Then your pattern is wrong. How about the following:
local function handler(msg, editbox) -- Grab all non-space tokens into a table local tokens = {} for token in msg:gmatch("%S+") do tokens[#tokens+1] = token end local command = tokens[1] if command == "add" and tokens[2] and tokens[3] then print(string.format("Add: %s, %s", tokens[2], tokens[3])) elseif command == "remove" and tokens[2] and tokens[3] then print(string.format("Remove: %s, %s", tokens[2], tokens[3])) else print("Syntax: /yourcmd (add|remove) someIdentifier"); end end
And you can do easier?
-
Posted by jon on Mon, 27 Feb 2012 20:37:48
function razdelitell(msg) local tokens = {} for token in msg:gmatch("%S+") do tokens[#tokens+1] = token end end local function handler(msg, editbox) local command = razdelitell(msg) if command == "add" and tokens[2] and tokens[3] and tokens[4] then SendChatMessage(string.format("Add: %s, %s, %s", tokens[2], tokens[3], tokens[4])) elseif command == "remove" and tokens[2] and tokens[3] then print(string.format("Remove: %s, %s", tokens[2], tokens[3])) else print("Syntax: /yourcmd (add|remove) someIdentifier"); end end
I'm trying to do and it does not work: (
-
Posted by jnwhiteh on Mon, 27 Feb 2012 20:40:45
What you wrote makes no sense. Use what I wrote. Or att return tokens to the end of the extra function you added.
-
Posted by jon on Mon, 27 Feb 2012 20:46:36
What you wrote makes no sense. Use what I wrote. Or att return tokens to the end of the extra function you added.
I'll use what you have written. But I want to learn new things. Help please ::)
-
Posted by jnwhiteh on Mon, 27 Feb 2012 20:48:45
Add "return tokens" to the end of razdelitell and that should work too.
-
Posted by jon on Mon, 27 Feb 2012 20:52:36
thank you
-
Posted by jnwhiteh on Mon, 27 Feb 2012 21:45:22
Do not do that to your posts. Just leave them.