How to read a moon table in C as follows

Asked

Viewed 174 times

1

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

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

I am the 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 moon now.

  • 1

    I don’t understand your question right. What is the problem you are having with your current code?

  • The following error appears at reading time, PANIC: unprotected error in call to Lua API

  • In which line does the error occur? (if you do not know, you can run the C program in a debugger to find out)

  • Doesn’t say what line.

  • You know how to use some debugger for C, they tell you the error line for you. Which operating system / IDE you are using?

  • I used here and pointed me here. lua_getfield(L, -1, "Table"); So this code Table = ae is inside the table.lua file, I’m trying to get it to read the values from the table there.

  • See if this helps in any way: http://answall.com/q/55230/101

  • I tried using this most unsuccessful

Show 3 more comments

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.

  • I was able to do the reading, but I found a problem, it makes the reading of each value, now I wanted to know how I can print each value that is being read, because var1 is printing the quantity only. Here the code, got big so I’m posting the link http://pastebin.com/sYWkthn1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.