lua-users home
lua-l archive

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


Am 21.07.2016 um 21:50 schrieb Russell Haley:
> 
> Perhaps posting an example for people to follow would help lessen some
> of the "noise"? I've written some neat stuff in Lua but would have no
> clue how to move forward with your suggestion. Maybe the people making
> the noise aren't as experienced as you are?
> 
My experience is below most of yours. But i try to find the door. Again:
1) try avoiding holes
2) if you cant because you did not write the code, try other modules or
   patch them.
3) if 1) and 2) cant help: rework the table you get and remove all holes.

This function should work for all tables:
--================================================================

function reworkTable(tbl)
  local result = {};
  local sorttbl = {};
  local lastIdx;
  for n, v in pairs(tbl) do
    if type(n) == "number" then
      if not lastIdx or (lastIdx + 1) ~= n then
        table.insert(sorttbl, {n,{v}});
      else
        table.insert(sorttbl[#sorttbl][2], v);
      end;
      lastIdx = n;
    else
      result[n] = v;
    end;
  end;
  table.sort(sorttbl, function(a,b) return a[1] < b[1]; end);
  for _, t in ipairs(sorttbl) do
    for _, v in ipairs(t[2]) do
      table.insert(result, v);
    end;
  end;
  return result;
end;

--================================================================