lua-users home
lua-l archive

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


Hi everyone,

I've been trying to read a table/array from lua in a C++ program without
success... I just want to get users list from a file like this:
------------------> BEGIN: users.db
	Entry{
		login = "lostuser"
	}
	Entry{
		login = "ohmygod"
	}
------------------< END: users.db

So, I created one function to do this task:

------------------> BEGIN: myFunctions.lua
	function GetUsersList ()
	    local loginList = {}  -- I tried without keyword `local' too
	    function Entry (b) -- See "Programming in Lua - Chapter 12"
	           loginList[b.login] = true
	    end
	    dofile("users.db")
	    for name in pairs(loginList) do print(name) end
	    return loginList
	end
------------------< END: myFunctions.lua

The output is correct:
	ohmygod
	lostuser

Bellow is what I'm trying to do in C++ to read the
values returned from the lua function:

//////////////////////// BEGIN: C++ snippet
    lua_getglobal(m_pLuaState, "GetUsersList");
    int result = lua_pcall(m_pLuaState, 0, 1, 0);
    if (! lua_istable(m_pLuaState, 1)) {
	// error ... bla bla bla
        return false;
    }
    // the next function will return 0 (zero).
    // It should return 2 (two).... WHY NOT???
    int n = (int) luaL_getn(m_pLuaState, 1);   

    // Thus it is impossible to start the next loop!
    for (int i = 1; i <= n; i++) {
         // I don't know if the next 2 rows will work,
         // HOW IS THE RIGHT WAY TO GET THE VALUES ???
         lua_rawgeti(m_pLuaState, 1, i);
         cout << (const char*) lua_tostring(m_pLuaState, -1) << endl;
    }
//////////////////////// END: C++ snippet

Does anyone could help me?
Thanks
   
Alexander Miro
Rio de Janeiro - Brazil

**********************************************************************
Informação transmitida destina-se apenas à pessoa a quem foi endereçada e pode conter informação confidencial, legalmente protegida e para conhecimento exclusivo do destinatário. Se o leitor desta advertência não for o seu destinatário, fica ciente de que sua leitura, divulgação ou cópia é estritamente proibida. Caso a mensagem tenha sido recebida por engano, favor comunicar ao remetente e apagar o texto de qualquer computador.


The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon this information, by person or entity other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from any computer.
**********************************************************************