2

I have the lua string.

strng = '{{1,2,3},{4,5,6},{7,8,"9+i"}}'

I want to convert strng to table.I know the following solution.

strng = loadstring("return "..strng)()

This solution works when used on strng. However it fails when it is given input from some other function. What I mean is this.

strng = loadstring("return "..somefunction(x))()

Here somefunction(x) gives the string output as {{1,2,3},{4,5,6},{7,8,"9+i"}} . So it doesn't work in this case. I am seeking for general solution, not for this particular string. However the output will always be of same type( I mean table string).

Maths89
  • 476
  • 3
  • 15
  • 5
    Please provide an example of case in which it does not work and its output. It works just fine with a: `loadstring("return " .. (function () return "{{1, 2, 3}, {4}}" end)())()` – Aki Jul 26 '19 at 10:46
  • 1
    Traverse your whole table and replace all strings with evaluated results. – Egor Skriptunoff Jul 26 '19 at 11:29
  • 1
    In combination with [this question](https://stackoverflow.com/questions/57215092/replacing-strings-in-lua-containing-special-characters), I guess: You have a string, you'll replace patterns in this string, so that it looks like a lua table and then you want to convert the table string to a lua table. So why you don't do some lua pattern matching and directly fill the values into a lau table? – csaar Jul 26 '19 at 13:45
  • `strng = loadstring("return "..somefunction(x))()` this code is correct; if it doesn't work, something's wrong with `somefunction` – DarkWiiPlayer Jul 29 '19 at 09:44

0 Answers0