lua-users home
lua-l archive

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


You could probably do something along the lines of:

f = assert( io.open("table.txt") )
s = f:read("*a")
f:close()
t = assert( loadstring("{"..s.."}") )()

I think this will work in 5.1. But in 5.2 you'll need to do something else for loadstring().

Robby

On Jan 25, 2014 7:02 PM, "jseb" <gmane2010@finiderire.com> wrote:
Hello,

I'd like to create a table, which contains sub-tables, from a file.

Here is my test file:

$ 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"}


And here is my Lua source file:

$ cat do_table.lua
#!/usr/bin/env lua

local t={}

for line in io.lines("table.txt") do
  t[#t+1]={line}
end

for _,subtable in pairs(t) do
  for k,v in pairs(subtable) do
    print(k,v)
  end
end


If i launch this script, i get this:
$ ./do_table.lua
1       foo = { "value foo 1", "value foo 2", name="i'm foo"}
1       bar = { "value bar 1", "value bar 2", name="i'm bar"}

but that's not what i expected.
I want to have this, instead:

1       foo = table 0x12345689
2       bar = talbe 0x23456780

Is this a problem of interpretation of the line i read ?

Thank you.