1

How can i transform this following string:

string = "1,2,3,4"

into a table:

table = {1,2,3,4}

thanks for any help ;)

droppels
  • 61
  • 1
  • 5
  • Does this answer your question? [Split a string using string.gmatch() in Lua](https://stackoverflow.com/questions/19907916/split-a-string-using-string-gmatch-in-lua) – Alexander Mashin Oct 22 '20 at 03:10
  • no, i tried string.gmatch but it transforms the table in `table = {"1,2,3,4"}` i want only `table = {1,2,3,4}`, without quotation marks – droppels Oct 22 '20 at 03:34
  • wait wait, i found how to do this: ```t = {}; s = "1,2,3,4" for w in (s .. ","):gmatch("([^,]*),") do table.insert(t, w) end for k, v in pairs(t) do tonumber(t[k]) end``` SOLVED! thank u – droppels Oct 22 '20 at 03:43
  • @droppels - Do not forget to `tonumber` these strings you have now in a table – Egor Skriptunoff Oct 22 '20 at 11:44
  • @droppels this pattern `([^,]*),` will miss the last entry. for example if input is `1,2,3,4` then `4` does not have a following `,` and would not match the pattern and would be exluded. – Nifim Oct 22 '20 at 15:24

3 Answers3

1

Let Lua do the hard work:

s="1,2,3,4"
t=load("return {"..s.."}")()
for k,v in ipairs(t) do print(k,v) end
lhf
  • 70,581
  • 9
  • 108
  • 149
0

Below is the code adapted from the Scribunto extension to MediaWiki. It allows to split strings on patterns that can be longer than one character.

-- Iterator factory
function string:gsplit (pattern, plain)
    local s, l = 1, self:len()
    return function ()
        if s then
            local e, n = self:find (pattern, s, plain)
            local ret
            if not e then
                ret = self:sub (s)
                s = nil
            elseif n < e then
                -- Empty separator!
                ret = self:sub (s, e)
                if e < l then
                    s = e + 1
                else
                    s = nil
                end
            else
                ret = e > s and self:sub (s, e - 1) or ''
                s = n + 1
            end
            return ret
        end
    end, nil, nil
end

-- Split function that returns a table:
function string:split (pattern, plain)
    local ret = {}
    for m in self:gsplit (pattern, plain) do
        ret [#ret + 1] = m
    end
    return ret
end

-- Test:
local str = '1,2, 3,4'
print ('table {' .. table.concat (str:split '%s*,%s*', '; ') .. '}')

Alexander Mashin
  • 3,892
  • 1
  • 9
  • 15
0

You can use gmatch and the pattern (%d+) to create an iterator and then populate the table.

local input = "1,2,3,4"
local output = {}

for v in input:gmatch("(%d+)") do
  table.insert(output, tonumber(v))
end

for _,v in pairs(output) do
  print(v)
end

The pattern (%d+) will capture any number of digits(0-9).

This is a narrow solution, it does not handle blank entries such as

input = "1,2,,4"
output = {1,2,4}

It also does not care what the delimitator is, or if it is even consistent for each entry.

input = "1,2 3,4"
output = {1,2,3,4}

Reference:

Lua 5.3 Manual, Section 6.4 – String Manipulation: string.gmatch

FHUG: Understanding Lua Patterns

Nifim
  • 4,758
  • 2
  • 12
  • 31