#table will equal 3
It will if you’re lucky, but it’s NOT guaranteed. The length operator only works as expected for sequences. Once you’ve violated that, all bets are off.
You could do
function GetCount (t, pred) local count = 0 for \_, v in pairs(t) do -- if pred(v) then -- call your "filled out" predicate here, if you want count = count + 1 -- end end return count end
if you only have integer keys like above, or
function GetCount (t, pred) local count = 0 for i = 1, table.maxn(t) do if t[i] ~= nil -- and pred(t[i]) -- As per the pairs() version then count = count + 1 end end return count end
otherwise.