lua-users home
lua-l archive

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



On Friday, January 23, 2004, at 11:21 AM, Virgil Smith wrote:

Your code....

    lua_pushstring(state,"a1");
    lua_gettable(state,LUA_GLOBALSINDEX);
    lua_pushstring(state,"a2");
    lua_gettable(state,LUA_GLOBALSINDEX);
    lua_settable(state,LUA_GLOBALSINDEX);

Implies to me that you missed the fact that tables in Lua are assigned by reference in Lua. The above code results in a Global variable whose "name" is the vector referenced by "a1". It does NOT overwrite the vector a1 with
the contents of the vector a2.  That statement might confuse the
uninitiated. Remember that global variables are merely entries in a Lua table and that Lua tables may contain entries whose keys are "any" value, not just strings and numbers. Thus a settable operation given a table for the key and a table for the value will NOT overwrite the key table it will
use the table AS A KEY.

The following example is a bit simpler....

    lua_pushstring(state,"a1");
    lua_pushstring(state,"a2");
    lua_gettable(state,LUA_GLOBALSINDEX);
    lua_settable(state,LUA_GLOBALSINDEX);

<smacks forehead> Right. I read that, and then promptly forgot about it. This works fine.

However, I must still be missing something because building a nested table still does not work. Here is some sample code (assume that a1 and a2 are properly defined tables):

   lua_pushstring(state,"nested"); -- name for the nested table
   lua_newtable(state);
   lua_pushstring(state,"bz");
   lua_pushstring(state,"a1");
   lua_gettable(state,LUA_GLOBALSINDEX);
   lua_settable(state,-3);
   lua_pushstring(state,"by");
   lua_pushstring(state,"a2");
   lua_gettable(state,LUA_GLOBALSINDEX);
   lua_settable(state,-3);
   lua_pushliteral(state,"n");
   lua_pushnumber(state,2);
   lua_rawset(state,-3);
lua_settable(state,LUA_GLOBALSINDEX); -- Assign the new table to "nested"

What I think this should do in Lua is:

   nested = {bz=a1, by=a2}

where a1 and a2 are predefined tables (globals). HOWEVER, the following *does* work:


   lua_pushstring(state,"nested"); -- name for the nested table
   lua_newtable(state);
   lua_settable(state,LUA_GLOBALSINDEX);
   lua_pushstring(state,"nested");
   lua_gettable(state,LUA_GLOBALSINDEX);
   lua_pushstring(state,"bz");
   lua_pushstring(state,"a1");
   lua_gettable(state,LUA_GLOBALSINDEX);
   lua_settable(state,-3);
   lua_pushstring(state,"by");
   lua_pushstring(state,"a2");
   lua_gettable(state,LUA_GLOBALSINDEX);
   lua_settable(state,-3);

Conceptually, I don't see how the two samples are different. I feel like I've found an acceptable work-around, but I'm missing something important in not understanding the difference in approaches. Thoughts?


=====================================================
Thomas Tongue <TTongue@imagiware.com>
Imagiware Inc. - http://www.imagiware.com/
Internet Presence and Consulting Services
== PGP Public Key: https://ssl1.imagiware.com/ttongue/public.key ==