lua-users home
lua-l archive

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


t = {f="f1", s="s1"}                         -- t is a table
t = {{f="f1", s="s1"}, {f="f2", s="s2"}}     -- t is a vector of tables
t = {{f="f1", s="s1"}}                       -- t is a vector with a single
table

If you want to support the #2 and #3 here is a code snippet.


const char *LuaGetTable(char *item)
{
  const char *s = NULL;
  lua_pushstring(S, item);
  lua_gettable(S, -2);
  if (lua_isstring(S, -1))
  {
    s = lua_tostring(S, -1);
  }
  lua_pop(S, 1);
  return s;
}

.
.
.

  lua_getglobal(S, "t");
  if (lua_istable(S, -1))
  {
    int i;
    int n = lua_getn(S, -1);
    for (i = 1; i <= n; i++)
    {
      lua_rawgeti(S, -1, i);
      printf("t.f = %s\n", LuaGetTable("f"));
      printf("t.s = %s\n\n", LuaGetTable("s"));
      lua_pop(S, 1);
    }
  }

-- Danilo