lua-users home
lua-l archive

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


On Sat, Jan 25, 2014 at 2:01 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.





It's doing exactly what you've told it: reading each line of a text file and storing it in a table. Nowhere did you ask it to interpret those lines.

If you can trust the input, you can use loadfile() to execute the entire file. Otherwise you need to parse it and build the table yourself.

--
Sent from my Game Boy.