Sometimes it just makes sense to use associative tables, but # can’t accurately return the length of assoc tables that contain ‘holes’ so I’ve been using a custom function for that instead:
module.tableLength = function(t) local iteration = 0 for k,v in pairs(t) do iteration = iteration + 1 end return iteration end
For my own needs so far, this approach has worked fine, but I’m conscious that if given a huge table it’s going to take a considerable time to churn over it and return a size.
Therefore just wondering if anybody has a better approach to this? I considered using custom wrapper functions to add to and remove from assoc tables which could then increment/decrement an internal counter to return instead of having to actually count when asked, but this approach seems risky - sods law I’ll just forget to use the wrapper one time and skew the count. Additionally the overhead of updating a counter every single time a change is made might counter the increased speed of the occasional count anyway…?