Question

In Lua, is there any way to split this string:

etc3=1336,etc2=14477,etc4=1335,etc1=1337

into this table?

tbl = {
    { 'etc3', 1336 },
    { 'etc2', 14477 },
    { 'etc4', 1335 },
    { 'etc1', 1337 },
}

Any help is appreciated.

Was it helpful?

Solution

local str = 'etc3=1336,etc2=14477,etc4=1335,etc1=1337'
local tbl = {}
for k, v in str:gmatch'(%w+)=(%d+)' do
  tbl[#tbl+1] = {k, tonumber(v)}
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top