lua-users home
lua-l archive

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


On 3/2/06, Matt Campbell <mattc@freedombox.cc> wrote:
The only way I know to count non-indexed values in a table is like this:

function countFields(t)
  local count = 0
  for k, v in pairs(t) do
    count = count + 1
  end
  return count
end

Does anyone have a better solution?

Yes, hopefully there is a better way to do this as that has a lot more overhead (especially on large arrays).  Since I figure internally  Lua at least knows the number of items in the hash table I'm wondering how to get to that value (which should be at least as fast as the length operator).

--
// Chris