Convert String to Tables

2
local items = {}
local t = "7 2182, 4 2554, 5 9908"

I wanted to convert using this string and let the table go like this:

local items = {{7,2182},{4,2554},{5,9908}}

Is there a way? using gsub maybe?

    
asked by anonymous 12.03.2017 / 01:02

1 answer

2

Try the code below:

local items = {}
local t = "7 2182, 4 2554, 5 9908"
local n = 0
for a,b in t:gmatch("(%d+)%s+(%d+)") do
        n = n + 1
        items[n] = {a,b}
end
for k,v in ipairs(items) do print(k,v[1],v[2]) end

The pattern in t:gmatch captures two digit sequences separated by spaces.

    
13.03.2017 / 17:16