From f6fb0fd7f02657e6a0d35e9acd2cae42d833a28c Mon Sep 17 00:00:00 2001 From: Rapptz Date: Thu, 15 Jan 2015 11:53:15 -0500 Subject: [PATCH] Improve formatting of table example --- examples/tables.cpp | 60 ++++++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/examples/tables.cpp b/examples/tables.cpp index 17c9e117..be2dfde9 100644 --- a/examples/tables.cpp +++ b/examples/tables.cpp @@ -2,35 +2,35 @@ #include #include -/// this example shows how to read data in from a lua table -using namespace std; +// this example shows how to read data in from a lua table -//compiles on linux with: g++ -std=c++11 -I.. table.cpp -o tables -llua +int main() { + sol::state lua; + // table used as an array + lua.script("table1 = {\"hello\", \"table\"}"); + // table with a nested table and the key value syntax + lua.script("table2 = {" + "[\"nestedTable\"] = {" + "[\"key1\"] = \"value1\"," + "[\"key2\"]= \"value2\"" + "}," + "[\"name\"]= \"table2\"" + "}"); -int main(int argc, char * argv[]) -{ - sol::state lua; - //table used as an array - lua.script("table1 = {\"hello\", \"table\"}"); - //table with a nested table and the key value syntax - lua.script("table2 = {[\"nestedTable\"]={[\"key1\"]=\"value1\", [\"key2\"]=\"value2\"}," - " [\"name\"]=\"table2\"}"); - - - //using the values stored in table1 - cout << lua.get("table1").get(1)<< " " << - lua.get("table1").get(2) << endl; - - auto t2 = lua.get("table2"); - auto nestedTable = t2.get("nestedTable"); - //some retrival of values from the nested table - //the cleaner way of doing things - cout << "nested table: key1 : " << nestedTable.get("key1") << ", key2: " - //yes you can chain the get<>() results - << lua.get("table2").get("nestedTable").get("key2") - << endl; - - cout << "name of t2: " << t2.get("name") << endl; - - return 0; -} \ No newline at end of file + + // using the values stored in table1 + std::cout << lua.get("table1").get(1) << " " + << lua.get("table1").get(2) << '\n'; + + auto t2 = lua.get("table2"); + auto nestedTable = t2.get("nestedTable"); + + // some retrieval of values from the nested table + // the cleaner way of doing things + std::cout << "nested table: key1 : " << nestedTable.get("key1") << ", key2: " + // yes you can chain the get<>() results + << lua.get("table2").get("nestedTable").get("key2") + << '\n'; + + std::cout << "name of t2: " << t2.get("name") << '\n'; +}