什么是Python list.pop()的lua等价物?
我正在做一个项目,最终用户将运行Lua,并与用Python编写的服务器进行通信,但我找不到一种方法来做Lua中需要做的事情 我向程序提供以下输入:什么是Python list.pop()的lua等价物?,python,list,lua,equivalent,Python,List,Lua,Equivalent,我正在做一个项目,最终用户将运行Lua,并与用Python编写的服务器进行通信,但我找不到一种方法来做Lua中需要做的事情 我向程序提供以下输入: recipient command,argument,argument sender 我得到一个列表的输出,其中包含: {"recipient", "command,argument,argument", "sender"} 然后,将这些项目分离为单个变量。然后,我将命令、参数、参数分离到另一个列表中,并再次将它们分离到变量中 我是如何用Pyth
recipient command,argument,argument sender
我得到一个列表的输出,其中包含:
{"recipient", "command,argument,argument", "sender"}
然后,将这些项目分离为单个变量。然后,我将命令、参数、参数
分离到另一个列表中,并再次将它们分离到变量中
我是如何用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
标题中的问题要求的是一些非常具体的东西:Lua等价于从数组中获取值并将其删除。这将是:
theTable = {}; --Fill this as needed
local theValue = theTable[1]; --Get the value
table.remove(theTable, 1); --Remove the value from the table.
您在文章中提出的问题似乎非常开放。您标题中的问题要求非常具体的东西:Lua等价于从数组中获取值并将其删除。这将是:
theTable = {}; --Fill this as needed
local theValue = theTable[1]; --Get the value
table.remove(theTable, 1); --Remove the value from the table.
你在文章中提出的问题似乎非常开放。如果我是你,我不会尝试按原样移植Python代码。这里有一个更简单的方法可以在Lua中实现同样的功能:
local test = "server searching,123,456 Guy"
local recipient,cmd,args,id = s:match("(.+) (.-),(.+) (.+)")
在这一步之后,recipient
是“server”;cmd
是“search”;args
是“123456”;id
是“Guy”
我真的不明白你想用locals()[“arg”+str(argument)]
做什么,显然你没有发布所有的代码,因为一直访问本地arg1
有点无用。。。但是,如果要迭代参数,只需使用string.gmatch
:
for arg in args:gmatch("[^,]+") do
-- whetever you want with arg
end
如果我是你,我就不会尝试按原样移植Python代码。这里有一个更简单的方法可以在Lua中实现同样的功能:
local test = "server searching,123,456 Guy"
local recipient,cmd,args,id = s:match("(.+) (.-),(.+) (.+)")
在这一步之后,recipient
是“server”;cmd
是“search”;args
是“123456”;id
是“Guy”
我真的不明白你想用locals()[“arg”+str(argument)]
做什么,显然你没有发布所有的代码,因为一直访问本地arg1
有点无用。。。但是,如果要迭代参数,只需使用string.gmatch
:
for arg in args:gmatch("[^,]+") do
-- whetever you want with arg
end
假设lua的split()创建了一个表,那么这就行了。需要额外的一行,但没关系。除非您使用提供它的某个Lua模块,否则Lua没有
split
函数。它有模式匹配,你可以用它来达到同样的效果。我发现一个页面包含多种分割功能,所以我真正需要的就是pop。我敢肯定,再多搜索一点,我就可以为locals()找到一些东西。假设lua的split()创建了一个表,那么这就行了。需要额外的一行,但没关系。除非您使用提供它的某个Lua模块,否则Lua没有split
函数。它有模式匹配,你可以用它来达到同样的效果。我发现一个页面包含多种分割功能,所以我真正需要的就是pop。我敢肯定,再多搜索一点,我就能为当地人找到一些东西()。