#include <iostream>
#include <string>
#include <lua.hpp>
int main() {
// Step 1: Create a Lua state
lua_State* L = luaL_newstate();
// Step 2: Load standard Lua libraries
luaL_openlibs(L);
// Step 3: Run Lua code to create a table
luaL_dostring(L, "my_table = {name='John', age=25, city='New York'}");
// Step 4: Access the table values
lua_getglobal(L, "my_table");
// Step 5: Accessing table fields by key
lua_getfield(L, -1, "name");
const char* name = lua_tostring(L, -1);
lua_pop(L, 1);
lua_getfield(L, -1, "age");
int age = static_cast<int>(lua_tonumber(L, -1));
lua_pop(L, 1);
lua_getfield(L, -1, "city");
const char* city = lua_tostring(L, -1);
lua_pop(L, 1);
// Step 6: Display the table values
std::cout << "Name: " << name << std::endl;
std::cout << "Age: " << age << std::endl;
std::cout << "City: " << city << std::endl;
// Step 7: Close the Lua state
lua_close(L);
return 0;
}