Module:table/maxIndex
Itsura
- This module lacks a documentation subpage. Please create it.
- Useful links: root page • root page’s subpages • links • transclusions • testcases • sandbox
local math_module = "Module:math"
local pairs = pairs
local function is_positive_integer(...)
is_positive_integer = require(math_module).is_positive_integer
return is_positive_integer(...)
end
--[==[
Returns the highest positive integer index of a table or array that possibly has holes in it, or otherwise 0 if no positive integer keys are found. Note that this differs from `table.maxn`, which returns the highest positive numerical index, even if it is not an integer.]==]
return function(t)
local max_key = 0
for k in pairs(t) do
-- The `strict_types` flag prevents type coercion to numbers.
if is_positive_integer(k) and k > max_key then
max_key = k
end
end
return max_key
end