lua-users home
lua-l archive

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


 > >I have a C function that expects char *argv[] as an argument.
 > >The natural representation for argv in Lua would be as a table
 > >with elements 1, 2, 3, etc.
 > >
 > >What is the recommended method of getting access to the array elements
 > >from within my C code?

 >  for (i=0; i<N; i++)
 >  {
 >   lua_Object v;
 >   v=lua_param(i)
 >   if (v==LUA_NOOBJECT || lua_isnil(v)) break;
 >   argv[i]=lua_getnumber(v);
 >  }

I must not have stated my question very carefully.
I have something like this for treating a table as an array:

<<print the elements in [[list]] as indexed by small integers>>=
for (i = 1; ; i++) {
  lua_Object o;
  lua_pushobject(list);
  lua_pushnumber((double)i);
  o = lua_getsubscript();
  if (lua_isnil(o)) break;
  s = lua_getstring(o);
  if (!s) lua_error("argument is not a string");
  printf("  %s\n", s);
}


but there is no guarantee that a table I am passed actually represents
an array.  I can't see how to find the number of non-nil entries in a
table, which is also awkward if I want to use tables to represent
sequences----it becomes difficult to write put, get, or append.

Norman