Lua and C + + Interactive learning record four: Global table interaction

Source: Internet
Author: User

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.c++ reading a table in Lua

① set a global table in Hello.lua:

1 2.34 " test_string "}

② read in C + +

1         //lua->stack, get global table, position -12Lua_getglobal (L,"global_c_read_table");3 4         //-------------------Get the first value----------------------------5         //C->STATCK, set the key value, position 1 (above-1 to-2)6Lua_pushstring (L,"Integer_val");7 8         //Lua->statck, the 1-bit key value pops up, the value of the corresponding key is obtained from LUA, and the result is placed in the position of-19         //If there is no value, the result is TnilTenLua_gettable (L,-2); One  A         //statck->c -         if(Lua_isinteger (L,-1)){ -printf"integer_val:%lld\n", Lua_tointeger (L,-1)); the         } -  -         //Popup-1-bit result, after which the global table's position is restored to-1 -Lua_pop (L,1); +  -         //-------------------Repeat the above steps to get the second value---------------------------- +Lua_pushstring (L,"Double_val"); ALua_gettable (L,-2); at         if(Lua_isnumber (L,-1)){ -printf"double_val:%g\n", Lua_tonumber (L,-1)); -         } -Lua_pop (L,1); -         //-------------------Repeat the above steps to get a third value---------------------------- -Lua_pushstring (L,"String_val"); inLua_gettable (L,-2); -         if(Lua_isstring (L,-1)){ toprintf"string_val:%s\n", Lua_tostring (L,-1)); +         } -Lua_pop (L,1);

2.c++ Writing tables to Lua

①c++ Write

1         //C->stack, create a new table and put it in the position of-12 lua_newtable (L);3 4         //C->stack, add a key, put it in the position of-1, the table position becomes-25Lua_pushstring (L,"Integer_val");6 7         //C->stack, add the value corresponding to the key, put in the-1 position, the key position becomes -2,table position becomes-38Lua_pushinteger (L,1);9         Ten         //set the key and Val to the table and eject the key and Val, where the table position becomes-1 OneLua_settable (L,-3); A  -         //Stack->lua, the table is assigned to LUA and the table pops up -Lua_setglobal (L,"global_c_write_table");

② in Hello.lua for LUA reading

1  for inch Pairs  Do 2     Print ("", K,"", v) 3 End

Lua and C + + Interactive learning record four: Global table interaction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.