String (g)Match

Asked

Viewed 82 times

3

Well, I’m looking to turn a string into a table. I tried so:

toset = "nb = 10, gb = 20, sb = 0, sfb = 0, ub = 0;"
toget = "nb = (.-), gb = (.-), sb = (.-), sfb = (.-), ub = (.-);"
t = toset:gmatch(toget)
for a,b,c,d,e in pairs(t) do
print(a.." "..b.." "..c.." "..d.." "..e)
end

But returns this error:

[string "stdin"]:4: bad argument #1 to 'pairs' (table expected, got function)

Can anyone handle it? Thank you.

  • 2

    gmatch does not work that way. Nor pairs.

1 answer

5


If you trust this string, then use

t = loadstring("return {"..toset.."}")()

Then you can check the contents of t with

for k,v in pairs(t) do
   print(k,v)
end

Browser other questions tagged

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