lua-users home
lua-l archive

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


On 04.08.2004 at 14:02 David Given wrote:

>I suspect that the real answer to your question is 'You don't want to do 
>that.' What are you trying to do?

I want to implement a function which creates a new table and initializes
it, e.g.

newarray(test, 100)

Now the C function newarray() should do the following Lua code:

test = {}
for i=1,100 do test[i] = 0 end

To do this, I somehow need to retrieve the string "test" in order
to push it on the stack and call lua_newtable(). 

My current function looks like this:
(The size and the name of the new table are currently hard-coded)

static int newarray(lua_State *L)
{
  int i;

  lua_pushstring(L, "test");
  lua_newtable(L);

  for(i = 1; i <= 100; i++) {
     lua_pushnumber(L, 0);
     lua_rawseti(L, -2, i);
  }
  
  lua_settable(L, LUA_GLOBALSINDEX);

  return 0;
}

The only solution I can think of is to pass the name for the new
table as a string to the newarray() function. Then it would work.
I could actually do it this way but I'm wondering if there's another solution...

Greets

Andreas
--
"Remember: It's nice to be important but it's more important to be nice!"