lua-users home
lua-l archive

Re: Iterating over tables

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


Hi Chris,
I think there are several problems for you to solve:
1.) It doesn't matter if you read in a Lua table in C or in Lua - the most important thing is that it's really a Lua table. So if you read it in with Lua, I think it will be a string. You can use the io functions
	io.open
	io.read
	...
for reading in the file. If you've done so, the file content will be in your Lua script as a string. 2.) The string must be parsed then to create the tables and subtables in Lua. Once you have tables, you can parse them :-) My suggestion would be to create a function named test () (according to your test file). The parameter to this function should be a table:
function test (tbl)
 if tbl ~= nil then
 print ("Table on board")
 r_print(tbl)
 return tbl
 end
 print ("Sad but true ;-(")
end
The function r_print () is just a recursive function for printing subtables:
function r_print (tbl)
 for i,v in pairs (tbl) do
 print (i,v)
 if type(v) == "table" then
	 r_print (v)
 end
 end
end
Use the loadstring () function for creating a function from your read in string and use pcall () for calling the string. It will call your test function, the table within can be accessed as function parameter :-):
s, error = loadstring ("test {admin = { x = 2, y = 3}}")
pcall (s)
As you can see here, there should be no "=" after test, because it shall be interpreted as function call. But you can also read in the string and prepend your functionname :
input = "test = {{admin = { x = 2, y = 3}}}"
input = "myfunction {" .. input .. "}"
Maybe that helps a little bit :-)
Regards,
Eva

AltStyle によって変換されたページ (->オリジナル) /