Convert a string to table without changing the order of the items

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Convert a string to table without changing the order of the items

Hendrik Meiring

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:

 

[1][+]

   [origin] (0, 0, 0)

   [uvector] (1, 0, 0)

   [vvector] (0, 1, 0)

 

Any quick solution for me?

 

Regards

Hendrik

Reply | Threaded
Open this post in threaded view
|

Re: Convert a string to table without changing the order of the items

Luiz Henrique de Figueiredo
> I need to convert this to a table but the order of the data must stay the same.

Try s:gsub('%["(%w+)"%]="(.-)",',print)
Replace print with a suitable function.

If the data file has more than one table, it'll take a little more work.

Reply | Threaded
Open this post in threaded view
|

Re: Convert a string to table without changing the order of the items

Duncan Cross
In reply to this post by Hendrik Meiring
On Thu, Apr 5, 2012 at 1:06 PM, Hendrik Meiring <[hidden email]> 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

Reply | Threaded
Open this post in threaded view
|

Re: Convert a string to table without changing the order of the items

Dirk Laurie-2
In reply to this post by Hendrik Meiring
>
    {{
    ["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.

I would do this in two stages:

First convert the string to this format, using gsub as in the other
suggestions:

{"vvector={0, 1, 0}", "uvector={1, 0, 0}",  "origin={0, 0, 0}", }

which can be brought in using loadstring as you do.  (Thanks to
the other replies this is an exercise for the reader.)  It evaluates
to an array of strings, preserving order.  Each entry can be
loadstringed by itself when needed.

I.e.

   t1 = [[ {"vvector={0, 1, 0}", "uvector={1, 0, 0}",  "origin={0, 0, 0}", } ]]
   loadstring("a="..t1)()
   loadstring(a[3])()
   print(origin[2])   --> 0