lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Christophe Jorssen wrote:
Hello all,

First of all, I'm quite new to lua so forgive me if
- this is a silly question,
- this is not the right place to ask such a silly question.

Hi,

I guess this is the right place to ask such a question.

I have the following code.

--
function traverse_table(t)
    if t then
       for k=1,#t do
	 if type(t[k] == "table") then

Here is the mistake, the parenthesis are wrong. I guess you want

  if type(t[k]) == "table" then

Otherwise Lua would first evaluate the equation t[k] == "table" which would return 'false' or 'true'. 'false' or 'true' is then passed to 'type' which would return the string "boolean". And a string (or any other value except false or nil) counts as true when used in an if-condition.

	    print("a table")
	    traverse_table(t[k])
	 else
	    print("not a table")
	 end
       end
    end
end