LUA, Separate string with numbers in a table!

5

I have a system to retrieve codes, in case I would put something like this in a text file:

HFGD65,{2454,2454},1,1

would be:

CODE to redeem - string {items} - items within the table in numbers points - numbers days - number

there I open: for example

local file = 'data/logs/codes.txt'
local f = io.open(file, "rb")
    for k, v in pairs(f) do
 -- retorna aquela linha
end

And I use

local v = ":JH2F36;:{{2173,1},{2160,2}};:0;:1;"
local var = v:gsub(':', ''):explode(';')

To be able to separate, but then I have to use the txt file like this:

:JH2F36;:{{2173,1},{2160,2}};:0;:1;

Is there a different way to call this line?

where would the code have to return string and the more in numbers?

If I separate by "," when I check the table in the middle and end there.

I wanted to simplify and find a way to put the code for example:

HB7S5S;{2173,2160}:1:1

how are 4 items there / \ separated by ":"

the first would return string (the code) - HB7S5S

and the rest (3) would return:

numbers:

(table)

1

1

    
asked by anonymous 10.03.2017 / 14:33

1 answer

1

You can separate the string with the function string.gmatch ()

function separa(s, delimitador)
tab = {};
for valores in (s..delimitador):gmatch("(.-)"..delimiter) do
    table.insert(tab, valores);
end
return result;
end

Or with the function string.match ():

function Separa(Cadeia)
     local res = {}
     for pares in string.match(Cadeia, "[^;]+")
     -- aqui também pode ler a linha inteira: "[^\r]*"
     do
        local eq = string.find(pares, "=")
        if eq 
        then
           local chave = pares:sub( 1, eq-1)
           local valor = pares:sub(eq+1,eq+8)

        end
     end
  end   
    
17.07.2017 / 21:46