lua 中与 Python 的 list.pop() 等价的函数是什么?

我正在开发一个项目,其中最终用户将运行 Lua,并与使用 Python 编写的服务器通信,但我无法找到在 Lua 中做我需要做的事情的方法。

我给程序输入:

收件人 命令,参数,参数 发件人

我得到一个包含以下内容的列表输出:

{"收件人", "命令,参数,参数", "发件人"}

然后,将这些项分成单独的变量。之后,我将 命令,参数,参数 分开到另一个列表中,并再次将它们分开到变量中。

我在 Python 中的做法:

test = "服务器 搜索,123,456 Guy" #示例
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

原文链接 https://stackoverflow.com/questions/7493923

点赞
stackoverflow用户734069
stackoverflow用户734069

你标题中的问题要求非常具体:获取 Lua 中的数组值并将其移除。代码如下:

theTable = {};  --请根据需求填写
local theValue = theTable[1];  --获取值
table.remove(theTable, 1);     --从表格中移除值。

但是,你在帖子中提出的问题似乎非常开放。

2011-09-21 03:12:51
stackoverflow用户204011
stackoverflow用户204011

如果我是你,我不会尝试直接移植 Python 代码。下面是在 Lua 中实现同样功能的更简单的方法:

local test = "server searching,123,456 Guy"
local recipient,cmd,args,id = s:match("(.+) (.-),(.+) (.+)")

经过这一步之后,recipient 是 "server",cmd 是 "searching",args 是 "123,456",id 是 "Guy"。

我不是很理解你试图用 locals()["arg" + str(argument)] 做什么,显然你没有发布所有代码,因为一直访问本地变量 arg1 对你来说有点无用。但如果你想迭代参数,可以使用 string.gmatch

for arg in args:gmatch("[^,]+") do
  -- 根据需求操作参数
end
2013-01-16 10:21:14