Module:parameters/remove holes

< Module:parameters
Revision as of 09:05, 6 August 2023 by Maria (talk | contribs) (Created page with "-- A helper function that removes empty numeric indexes in a table, -- so that the values are tightly packed like in a normal Lua table. -- equivalent to require("Module:table").compressSparseArray return function (t) local ret = {} local index = 1 local highest = 0 for num, _ in pairs(t) do if type(num) == "number" and num > 0 and num < math.huge and math.floor(num) == num then highest = math.max(highest, num) end end for i = 1, highest do if t[i] then...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:parameters/remove holes/documentation

-- A helper function that removes empty numeric indexes in a table,
-- so that the values are tightly packed like in a normal Lua table.
-- equivalent to require("Module:table").compressSparseArray
return function (t)
	local ret = {}
	local index = 1
	local highest = 0
	for num, _ in pairs(t) do
		if type(num) == "number" and num > 0 and num < math.huge and math.floor(num) == num then
			highest = math.max(highest, num)
		end
	end
	for i = 1, highest do
		if t[i] then
			ret[index] = t[i]
			index = index + 1
		end
	end
	return ret
end