lua-users home
lua-l archive

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



Am 03.12.2012 um 23:03 schrieb Coda Highland:

On Mon, Dec 3, 2012 at 2:02 PM, Coda Highland <chighland@gmail.com> wrote:
On Mon, Dec 3, 2012 at 1:56 PM, Philipp Kraus
<philipp.kraus@flashpixx.de> wrote:

Am 03.12.2012 um 22:44 schrieb Coda Highland:

On Mon, Dec 3, 2012 at 1:47 PM, Philipp Kraus
<philipp.kraus@flashpixx.de> wrote:
Hello,

I use lua_createtable for push std::vectors to my Lua function, it works
well.
I would like to push also the std::map as key-value structure to Lua,
so I have got

lua_createtable(m_lua, p_map.size(), 0);

for( typename std::map<std::string, int>::const_iterator it = p_map.begin();
it != p_map.end(); ++it )

{
lua_pushstring(m_lua, it->first.c_str());
lua_pushnumber(m_lua, it->second);
// should here a pop call on the stack?
}
// should here also a pop call on the stack?

Which calls must be added, so I can use a table in the script, that has a
structure
table["key"] = value ?


In this case another question is occured: Can I used also a std::multimap?
So can Lua handle tables with
duplicated keys or must be the keys unique?

Thanks

Phil


You'd use lua_setfield() after those two pushes, and that'll pop them
for you, and Lua keys have to be unique. Check the docs for more
details.

Thanks, but how can I use the setfield with a map like std::map<int, std::String>.
setfield need a char value.

Thanks

Phil



lua_settable if your key is a number.

/s/ Adam

Correction: if your key is a non-string, numbers aren't the only legal
non-string key type.

I have create this:

        lua_createtable(m_lua, p_map.size(), 0);
        for( typename std::map<std::string, int>::const_iterator it = p_map.begin(); it != p_map.end(); ++it )
        {
            lua_pushnumber(m_lua, it->second);
            lua_setfield(m_lua, -2, it->first.c_str());
}

I get a table in my Lua function, but this table is empty. What is my mistake?

Phil