Re: creating a table from a file
[
Date Prev][
Date Next][
Thread Prev][
Thread Next]
[
Date Index]
[
Thread Index]
- Subject: Re: creating a table from a file
- From: Robert Raschke <rtrlists@...>
- Date: 2014年1月27日 14:34:03 +0000
/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:
> 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