在 Lua 中从 gmatch 返回的列表创建数组

Sim*_*aur 5 arrays string lua lua-patterns lua-table

我正在用 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
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点。

Yu *_*Hao 6

之前声明表,并在循环中添加元素,如下所示:

local t = {}
for i in S:gmatch("([^,%s]+)") do  
    t[#t + 1] = i
end 
Run Code Online (Sandbox Code Playgroud)

该模式[^,%s]+匹配一​​个或多个非逗号、非空格字符。