lua-users home
lua-l archive

RE: LuaPas and array of record

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


Here we go, hopefully I don't fat finger anything in here :)
procedure LoadToArray(L : PLua_State; TableName : String; var LoadInto :
array of RecType);
var
 idxTable, idxTemp,
 idxSubTable : Integer;
 Key, SubKey : String;
 SubVal : Double;
begin
 SetLength(LoadInto, 0);
 lua_pushstring(L, PChar(TableName));
 lua_gettable(L, LUA_GLOBALSINDEX);
 if lua_type(L, -1) <> LUA_TTABLE then
 raise exception.Create('Table expected for identifier
"'+TableName+'"');
 idxTable := lua_gettop(L);
 lua_pushnil(L);
 while (lua_next(L, idxTable)<>0) do
 begin
 SetLength(LoadInto, Length(LoadInto)+1);
 Key := lua_tostring(L, -2);
 idxSubTable := lua_gettop(L);
 lua_pushnil(L);
 while (lua_next(L, idxSubTable)<>0) do
 begin
 SubKey := lua_tostring(L, -2);
 if CompareText(SubKey, 'f') = 0 then
 LoadInto[Length(LoadInto)-1].Name := lua_tostring(L, -1)
 else
 LoadInto[Length(LoadInto)-1].Value:= lua_tonumber(L, -1);
 lua_pop(L, 1);
 end;
 lua_pop(L, 1); { This one may not be necessary, I honestly can't
remember. If not an exception will be thrown :) }
 lua_pop(L, 1);
 end;
 lua_pop(L, 1);
end;
 - Jeremy
"Help I suffer from the oxymoron Corporate Security."
> -------- Original Message --------
> Subject: LuaPas and array of record
> From: Jilani Khaldi <jilani.khaldi1@virgilio.it>
> Date: Thu, June 29, 2006 1:14 pm
> To: Lua list <lua@bazar2.conectiva.com.br>
> 
> Hi All,
> now the big next step. I have this table of tables in Lua:
> tab={
> {f="a", v=1},
> {f="b", v=-3},
> {f="c", v=2}
> }
> and
> the array of record in Delphi:
> type
> RecType = record
> name: string[24];
> value: double;
> end;
> ArrType = array of RecType;
> 
> var
> a: ArrType;
> ...
> begin
> SetLength(a,3);
> How to do get this:
> a[0].name := tab[1].f;
> a[0].value := tab[1].v;
> a[1].name := tab[2].f;
> a[1].value := tab[2].v;
> a[2].name := tab[3].f;
> a[2].value := tab[3].v;
> end;
> It was so easy to access to lua single table values from Pascal 
> (translating the C code from the manual), but I couldn't find the right 
> way to solve this one.
> Seen I am using only LuaPas, I prefer to stay close to the C way to 
> access Lua code without using any OO wrappers...
> Thank you!
> 
> jk

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