[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: RE: Walk tables recursively in code
- From: jdarling@...
- Date: Thu, 29 Jun 2006 10:17:23 -0700
Well the final working code (about 11:00 last night) ended up being:
LuaGetTable(L, TableIndex, 'Params');
idxParams := lua_gettop(L);
while (lua_next(L, idxParams) <> 0) do
begin
Key := LuaToString(L, -2);
Value := LuaToString(L, -1);
LoadInto.Params.Values[Key] := Value;
lua_pop(L, 1); // Was missing this one
end;
lua_pop(L, 1);
- Jeremy
"Help I suffer from the oxymoron Corporate Security."
> -------- Original Message --------
> Subject: Re: Walk tables recursively in code
> From: Romulo Bahiense <romulo@icontroller.com.br>
> Date: Thu, June 29, 2006 8:28 am
> To: Lua list <lua@bazar2.conectiva.com.br>
>
> Hi.
>
> > 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;
>
> // You forgot to push nil =)
> lua_pushnil(L);
> > while (lua_next(L, -2)<>0) do
> > begin
> // Avoid using lua_tostring here because it can change the
> // value on the stack when the value is a number (thus, breaking
> // lua_next behavior)
> // LoadInto.Params.Values[LuaToString(L, -2)] := LuaToString(L,
> -1);
>
> if lua_isnumber(L, -2) then
> LoadInto.Params.Values[FloatToStr(lua_tonumber(L, -2))]:=
> lua_tostring(L, -1)
> else if lua_isstring(L, -2) then
> LoadInto.Params.Values[lua_tostring(L, -2)]:=
> lua_tostring(L, -1)
> else
> raise Exception.Create('duh');
>
> > lua_pop(L, 1);
> > end;
> > end;
>
> (Again, not tested ;))
>
> --rb