我正在用 Lua 编程,到目前为止我已经有了这个。
S=tostring(M[i].AllSegmentsList) --it returns "MSH, PID"
for i in string.gmatch(S, ",") do --I have ", " as delimiter
t= {} --Now, I want the values returned by delimiter to be added into an array.
end
我该怎么做。
最佳答案
先声明表格,然后在循环中添加元素,如下所示:
local t = {}
for i in S:gmatch("([^,%s]+)") do
t[#t + 1] = i
end
模式 [^,%s]+
匹配一个或多个非逗号、非空格字符。
关于arrays - 从 gmatch 返回的列表在 Lua 中创建数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21600411/