lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


/tmp $ cat table.txt
foo = { "value foo 1", "value foo 2", name="i'm foo"}
bar = { "value bar 1", "value bar 2", name="i'm bar"}
/tmp $ lua
Lua 5.2.2  Copyright (C) 1994-2013 Lua.org, PUC-Rio
> l={}
> for line in io.lines("table.txt") do
>>   l[#l+1] = line
>> end
> safe={}
> t = assert( loadstring("return {" .. table.concat(l, ",") .. "}", nil, nil, safe) )()
> for top, subtable in pairs(t) do
>>   for k, v in pairs(subtable) do
>>     print(top, k, v)
>>   end
>> end
foo    1    value foo 1
foo    2    value foo 2
foo    name    i'm foo
bar    1    value bar 1
bar    2    value bar 2
bar    name    i'm bar
>



On 27 January 2014 09:01, Peter Melnichenko <petjamelnik@yandex.ru> wrote:
On Mon, Jan 27, 2014 at 12:04 AM, Dirk Laurie <dirk.laurie@gmail.com> wrote:

>  2. If the input is not trustworthy, you can say
>  safe={}
>  t = assert( loadstring("return {"..s.."}",nil,nil,safe) )()

This isn't going to work. The string is just a list of assignments, there are no separators between key-value pairs, so "{"..s.."}" is not a correct table literal.

Peter