1

A table in lua is defined as below

tab = {"Sunday", 14.5, "Tuesday", "Wednesday",
        63, -92, "Saturday", "Saturday", 111}

Lua call a c function, and the tab table is set as a param, this c function should return the table after it has been updated

new_tab_result = call_c_function(..,tab)

I whould like to amend all string values and set them to "DEFAULT", and return the table after the amend to lua.

C Code

while (lua_next(L, 6) != 0)  
{
...
else if(lua_isstring(L, -1))     
{
    lua_pushstring(L, "DEFAULT");
    lua_replace(L, -2);
    k = luaL_checkstring(L, -1);
    log("%s",k) // "DEFAULT"

}
...
lua_pop(L, 1);
}

return 1;
}

Lua Code

for key,value in pairs(new_tab_result) do
  DebugLog(key.."-"..value)
end

result

 1-Sunday
 2-14.5
 3-Tuesday
 4-Wednesday
 5-63
 6--92
 7-Saturday
 8-Saturday
 9-111

String value still have the initial value, while it should has been defaulted to "DEFAULT"

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
Messady
  • 45
  • 2
  • 7

1 Answers1

2

lua_replace works on the stack, not on the table. Use lua_settable or lua_setfield.

lhf
  • 70,581
  • 9
  • 108
  • 149
  • How can i directly amend an existing value while iterating over the table ? – Messady Sep 19 '13 at 18:43
  • 2
    @Messady, push the table, push the key, push the value, call `lua_settable`; or push the table, push the value, call `lua_setfield`. – lhf Sep 19 '13 at 22:11