Icy North Technologies Forum Index Icy North Technologies
User Forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

for k, v in pairs (tbl) do

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Icy North Technologies Forum Index -> Language
View previous topic :: View next topic  
Author Message
Val42



Joined: 15 Dec 2009
Posts: 2

PostPosted: Mon Dec 21, 2009 6:33 pm    Post subject: for k, v in pairs (tbl) do Reply with quote

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 -
Back to top
View user's profile Send private message
lhf



Joined: 20 Feb 2006
Posts: 175
Location: Rio de Janeiro, Brazil

PostPosted: Mon Dec 21, 2009 6:41 pm    Post subject: Reply with quote

Use lua_next.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Icy North Technologies Forum Index -> Language All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group