How to read a moon table in C as follows

1

I am trying to create a moon reading that can read this table below, the table should only accept values and if they add string, it warns the location of the error.

Table = {
        1000,       2000,       3000,       4000,       5000,
        6000,       7000,       8000,       9000,       10000,
        11111,      12000,      13000,      14000,      15000,..
}

I'm code is as follows:

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

How can I read this table, I'm starting to learn the moon now.

    
asked by anonymous 24.03.2015 / 00:48

1 answer

1

Use lua_getglobal(L, "Table") instead of lua_getfield(L, -1, "Table") .

Consider using luaL_dofile instead of luaL_loadfile + lua_pcall .

    
25.03.2015 / 14:54