lua-users home
lua-l archive

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


> On the other hand, maxn works with sparse arrays only if they're not
> "sparse at the end", as it were. I personally think this is simply a
> bug waiting to get a user of maxn, and that people who actually use
> sparse arrays should come up with their own protocol to record the
> "size" of the array. I've gone back to using a 'n' field, myself...

Another approach is to use mask and unmask nil functions for accessing
the array. Then it's possible to just use the # operator without
modification:

untested:
Nil = {} -- notice captial 'Nil', not nil
function maskNil(v)
  return v~=nil and v or Nil
end
function unmaskNil(v)
  return v ~= Nil and v or nil
end
function add(t, v)
  t[#t+1] = maskNil(v)
end
function get(t, ind)
  return unmaskNil(t[ind])
end

etc...

-Paul