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"

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

How can i directly amend an existing value while iterating over the table ?
@Messady, push the table, push the key, push the value, call lua_settable; or push the table, push the value, call lua_setfield.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.