LUA, Separate string with numbers in a table!

Asked

Viewed 525 times

5

I have a code rescue system, 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

Then I open it up: 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;

there’s some different way to call that line?

the code would have to return string and the most in numbers?

if I sort by "," when you check the table cuts in half and ends there.

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

HB7S5S;{2173,2160}:1:1

how are 4 items there / separate by ":"

the first would return string(the code) -- HB7S5S

and the others(3) would return:

numbers:

(table)

1

1

  • Have you considered using regular expressions?

  • I don’t know how to use it, I don’t know if there’s a way to explain to me how to do it.

1 answer

2


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   

Browser other questions tagged

You are not signed in. Login or sign up in order to post.