[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Help me get the number of items in the table
- From: "szbnwer@..." <szbnwer@...>
- Date: Sat, 29 Jul 2017 13:47:15 +0200
you should write a function that walks through on a table with pairs()
and accumulates the steps
into a variable, because ipairs() and # only works on the array part,
so 1,2,3... indexes in a line from 1, but if there are nil-s before
another number keys, then thats something you should not relay on with
#, like {1,2,nil,4,nil,nil,nil,8} will give 8 for lenght because of
binary search, so that is searching exponentially... my implementation
works for strings and tables as well, use it as you like it, you can
even reduce the string part:
len=function(stuff)
local typ=type(stuff)
if typ=='string' then return #stuff
elseif typ=='table' then
local l=0
for _ in pairs(stuff) do
l=l+1 end
return l end end
have fun with lua, it's the best language out there :D