lua-users home
lua-l archive

Walk tables recursively in code

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


I thought that I had this working properly, but seems I've made some
mistakes. Basically I have a table of tables, and I want to walk all
levels by name.
So my lua table looks like:
XML = {CData = "", Name = "App:Settings", Text = "", SubItems = {Version
= {CData="", Name = "Version", Text = "", SubItems = {}, Params = {Num =
"1.0.0.89}}, Filter = {CData = "<code block>", Name = "Filter", Text =
"", SubItems = {}, Params = {}}}, Params = {ScriptLang = "LUA"}}
Now in my code (Delphi, but I can read/write C/C++):
procedure LuaToXML(L : PLua_State; TableIndex : Integer; LoadInto :
TXMLItem);
begin
 LoadInto.Name := LuaGetTableString(L, TableIndex, 'Name');
 LoadInto.Text := LuaGetTableString(L, TableIndex, 'Text');
 LoadInto.CData := LuaGetTableString(L, TableIndex, 'CData');
 lua_pushnil(L);
 lua_pushstring(L, 'Params');
 lua_gettable(L, TableIndex);
 LoadInto.Params.Clear;
// Blows up here
 while (lua_next(L, -2)<>0) do
 begin
 LoadInto.Params.Values[LuaToString(L, -2)] := LuaToString(L, -1);
 lua_pop(L, 1);
 end;
end;
function GetTableIndex(L : PLua_State; TableName : String) : Integer;
begin
 lua_pushstring(L, PChar(TableName));
 lua_gettable(L, LUA_GLOBALSINDEX);
 if lua_type(L, -1) = LUA_TTABLE then
 result := lua_gettop(L)
 else
 raise exception.create(TableName + ' is not a valid Lua Table');
 lua_pop(L, 1);
end;
This is just the first steps, but basically I'm loading an XML doc from
a lua table. I know that there are other ways of doing this, but for
my project I need to do it this way :). I'm guessing I have my
blinders on as I can't figure out the problem, and I know its a stupid
mistake on my part.
Lua 5.0.2
Thanks,
 - Jeremy
"Help I suffer from the oxymoron Corporate Security."

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