[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Convert a string to table without changing the order of the items
- From: Duncan Cross <duncan.cross@...>
- Date: Thu, 5 Apr 2012 14:17:52 +0100
On Thu, Apr 5, 2012 at 1:06 PM, Hendrik Meiring <hmeiring@emss.co.za> wrote:
> Hi,
>
> I have string data with the format as shown below:
>
> {{
> ["vvector"]="(0, 1, 0)",
> ["uvector"]="(1, 0, 0)",
> ["origin"]="(0, 0, 0)",
> },
> }
>
> I need to convert this to a table but the order of the data must stay the
> same.
>
> Using the following code:
>
> local gentables = loadstring("return "..s)
> local tables = assert(gentables)()
>
> generates a table as shown below:
I would try transforming the key-value pairs into array entries,
something like this:
local s2 = s:gsub('%[(".-")%]%s*=%s*(".-")', '{k=%1, v=%2}')
This converts:
["key"]="value"
...into:
{k="key", v="value"}
So the resulting table will be like this:
[1][+]
[1][+]
[k] origin
[v] (0, 0, 0)
[2][+]
[k] uvector
[v] (1, 0, 0)
[3][+]
[k] vvector
[v] (0, 1, 0)
-Duncan