[英]What is the lua equivalent of Python list.pop()?
I'm working on a project where the end-users will be running Lua, and communicating with a server written in Python, but I can't find a way of doing what I need to do in Lua. 我正在一个最终用户将运行Lua并与用Python编写的服务器进行通信的项目中,但是我找不到在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. 之后,我将
command,argument,argument
分成另一个列表,然后再次将它们分成变量。
How I did it in Python: 我是如何在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. 您标题中的问题是要问一些非常具体的问题:Lua等同于从数组中获取值并将其删除。 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. 如果我是你,我不会尝试按原样移植Python代码。 Here is a much simpler way to achieve the same thing in Lua:
这是在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". 在此步骤之后,
recipient
是“服务器”, cmd
是“搜索”, args
是“ 123,456”, id
是“ 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
: 我不太了解您要使用
locals()["arg" + str(argument)]
,显然您没有发布所有代码,因为一直访问本地arg1
有点用处...但是,如果要迭代参数,请使用string.gmatch
:
for arg in args:gmatch("[^,]+") do
-- whetever you want with arg
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.