Val42
Joined: 15 Dec 2009 Posts: 2
|
Posted: Mon Dec 21, 2009 6:33 pm Post subject: for k, v in pairs (tbl) do |
|
|
My experience: I'm a professional programmer in a few languages, but mostly use C++. I'm new to Lua. I have written a few things in Lua, but really just toy programs. I'm going to use C++ terminology because this is what I am most familiar with.
For debugging purposes, I started with an example and wrote the following code in Lua:
| Code: | tables = {};
function indent(lvl)
ind = "";
for i = 1, lvl-1 do
ind = ind .. "\t";
end;
return ind;
end;
function print_table(tbl, lvl)
tables[tbl] = true;
for k, v in pairs( tbl ) do
if( lvl <= 0 ) then
print(k, v);
else
print(indent(lvl), k, v);
end;
if( type(v) == "table" and not tables[v] ) then
print_table(v, lvl + 1);
if( getmetatable(v) ) then
print(indent(lvl + 1) .. "metatable ===>");
print_table(getmetatable(v), lvl+1);
else
print(indent(lvl + 1) .. "metatable ===> nil");
end;
end;
end;
end; |
I want to rewrite this code in C or C++. I've figured out the rest except for "for k, v in pairs( tbl )". Is this done by finding out the size of a table ("lua_objlen") then iterating through the table?
Thanks for your help,
- Val - |
|