I have a table as follows below.
Table = {
1, 2, 3,
6, 7, 8,
11, 12, 13,...
}
I want to read that table and say how many values were read, the more I'm having trouble with my code below, always the following error occurs when reading PANIC: unprotected error in call to Lua API
I am using lua 5.2, the error is pointed out here
lua_getfield (L, -1, "Table");
can someone help me with this reading, I'm new in lua.
void read_table(void) {
int var1;
long double var2;
lua_State *L;
L = = luaL_newstate();
luaL_openlibs(L);
if (luaL_loadfile(L, "table.lua") || lua_pcall(L, 0, 0, 0)) {
printf("cannot run cofig file: %s\n");
return;
}
else {
lua_getfield(L, -1, "Table");
if (lua_type(L, -1) == 5) {
var1 = 1;
while (1)
{
lua_pushnumber(L, var2);
lua_gettable(L, -2);
if (!lua_isnumber(L, -1))
break;
lua_tonumber(L, -1);
lua_settop(L, -2);
++var1;
lua_close(L);
}
printf("table %d invalid\n", var1);
}
else {
printf("table should be a table\n");
}
}
printf("Reading %d numbers\n", var1);
}