The main content is reproduced from: Zilong People Blog (strongly recommended to go to the sub-Longshan blog completely learn it again)
Part of the content is read from: "Lua 5.3 Reference Manual," the Chinese version of the translator Cloud wind production KAVCC
vs2013+lua-5.3.3
1. Arrays
The ① array is a table with a key value starting at 1 and incrementing continuously.
The ② index number is starting from 1, not starting from 0.
2. Since it is a table, it is of course possible to read and write with the contents of the previous section.
LUA also provides some APIs that operate on arrays only, as follows:
①lualib_api Lua_integer (Lual_len) (lua_state *l, int idx);
The number of array elements can be obtained by using the array.
Returns the "Length" of the value at a given index as a number, which is equivalent to invoking the ' # ' operation in Lua. (Non-array use # will not get the correct value)
If the action result is not an integer, an error is thrown. (This happens only when the triggering meta-method is used.) )
②lua_api Int (Lua_rawgeti) (lua_state *l, int idx, Lua_integer n);
Note the corresponding Lua_rawget function.
Gets the array value of index number n, which returns an int value of the type of the value.
This is a direct access that does not trigger a meta-method and is highly efficient.
③lua_api void (Lua_rawseti) (lua_state *l, int idx, Lua_integer n);
Note the corresponding Lua_rawset function
Sets the array value of index number n.
This function pops the value out of the stack. The assignment is straightforward, i.e. it does not trigger a meta-method and is highly efficient.
3.c++ reading an array in Lua
Array in ①lua
Global_c_read_array = {5,6.78,"array string"}
Read in ②c++
1 //Lua->stack, get global array, position -12Lua_getglobal (L,"Global_c_read_array");3 4 //get the array length5Lua_integer Array_len = Lual_len (L,-1); 6 for(Lua_integer i =1; I <= Array_len; ++i) {7 //lua->stack, global array position-1, then I is the corresponding index value, put the return value to the position of-18 intRet_type = Lua_rawgeti (L,-1, i);9 Ten //--------------The original operation table function that was substituted------------- One //Lua_pushinteger (L, i); A //int ret_type = lua_gettable (L,-2);//you can also use int ret_type = Lua_rawget (L,-2); - - //stack->c the if(Ret_type = =Lua_tnumber) { - if(Lua_isinteger (L,-1)){ -printf"%lld\n", Lua_tointeger (L,-1)); - } + Else if(Lua_isnumber (L,-1)){ -printf"%g\n", Lua_tonumber (L,-1)); + } A } at Else if(Ret_type = =lua_tstring) { -printf"%s\n", Lua_tostring (L,-1)); - } - -Lua_pop (L,1); -}
4.c++ writing to LUA arrays
①c++ Write
1 //C->stack, create a new table and put it in the position of-12 lua_newtable (L);3 4 //c->stack, create the corresponding Key-val5Lua_pushinteger (L,7);//Setting the value6Lua_rawseti (L,-2,1);//Correspondence Key-val7 8Lua_pushnumber (L,8.9);//Setting the value9Lua_rawseti (L,-2,2);//Correspondence Key-valTen OneLua_pushstring (L,"test_string");//Setting the value ALua_rawseti (L,-2,3);//Correspondence Key-val - - //--------------The original operation table function that was substituted------------- the //Lua_pushinteger (L, 1);//Set Key - //Lua_pushinteger (L, 7);//Setting the value - //lua_settable (L,-3);//You can also use Lua_rawset (l,-3); - + //Lua_pushinteger (L, 2);//Set Key - //Lua_pushnumber (L, 8.9);//Setting the value + //lua_settable (L,-3);//You can also use Lua_rawset (l,-3); A at //Lua_pushinteger (L, 3);//Set Key - //lua_pushstring (L, "test_string");//Setting the value - //lua_settable (L,-3);//You can also use Lua_rawset (l,-3); - - - //Stack->lua, assigns the array to Lua, and pops the array inLua_setglobal (L,"Global_c_write_array");
② Lua reads in the Hello.lua file
1 if Then 2 for 1 Do 3 Print ("", Global_c_write_array[i]) 4 End 5 End
Lua and C + + Interactive learning record five: global array interaction