lua-users home
lua-l archive

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


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);

Would result in the global variable "a1" referencing the SAME "vector" as
global variable "a2".  In other words, after this operation assigning to
a1[3] would overwrite a2[3].

For information concerning copying the contents of one table to another
table search the list archives and the wiki on "deep copy", "lazy copy", and
"lazy copy" and "RLake" together.  RLake is one of the list members and he
contributed a nice "Lazy Copy" routine implementation to the wiki.