I'm working on a project where the end-users will be running Lua, and communicating wi开发者_C百科th a server written in Python, but I can't find a way of doing what I need to do in Lua.
I give the program an input of:
recipient command,argument,argument sender
I get an output of a list containing:
{"recipient", "command,argument,argument", "sender"}
Then, separate those items into individual variables. After that, I'd separate command,argument,argument
into another list and separate those into variables again.
How I did it in Python:
test = "server searching,123,456 Guy" #Example
msglist = test.split()
recipient = msglist.pop(0)
msg = msglist.pop(0)
id = msglist.pop(0)
cmdArgList = cmd.split(',')
cmd = cmdArgList.pop(0)
while len(cmdArgList) > 0:
argument = 1
locals()["arg" + str(argument)]
argument += 1
The question in your title is asking for something very specific: the Lua equivalent of getting a value from an array and removing it. That would be:
theTable = {}; --Fill this as needed
local theValue = theTable[1]; --Get the value
table.remove(theTable, 1); --Remove the value from the table.
The question you ask in your post seems very open-ended.
If I were you I wouldn't try to port the Python code as is. Here is a much simpler way to achieve the same thing in Lua:
local test = "server searching,123,456 Guy"
local recipient,cmd,args,id = s:match("(.+) (.-),(.+) (.+)")
After this step recipient
is "server", cmd
is "searching", args
is "123,456" and id
is "Guy".
I do not really understand what you are trying to do with locals()["arg" + str(argument)]
, apparently you haven't posted all your code since accessing the local arg1
all the time is a bit useless... But if you want to iterate the arguments just use string.gmatch
:
for arg in args:gmatch("[^,]+") do
-- whetever you want with arg
end
精彩评论