lua-users home
lua-l archive

Simple question about traversing a table

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


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.
I have the following code.
--
function traverse_table(t)
 if t then
 for k=1,#t do
	 if type(t[k] == "table") then
	 print("a table")
	 traverse_table(t[k])
	 else
	 print("not a table")
	 end
 end
 end
end
T = { [1] = { [1] = "item1", [2] = "item2" }}
traverse_table(T)
print("***")
traverse_table(T[1])
print("***")
traverse_table(T[1][1])
--
AFAIU, traverse_table is called recursively *even* when reaching a
string element of the table (and that should be avoided thanks to the
type test).
I'd like to have the following output for traverse_table(T):
a table -- T[1] is a table
not a table -- T[1][1] is a string
not a table -- T[1][2] is a string
Have I missed something?
Note that it surprised me that traverse_table(T[1][1]) actually worked
treating a string like a table. I'll have to investigate PIL a bit
more :-)
Thanks in advance
-- 
Christophe

AltStyle によって変換されたページ (->オリジナル) /