Add table.hpairs

This commit is contained in:
Lars Mueller 2022-07-10 12:18:35 +02:00
parent d4c6468cec
commit 187723118c

@ -616,6 +616,20 @@ function rpairs(table)
end end
end end
-- Iterates the hash (= non-list) part of the table. The list part may not be modified while iterating.
function hpairs(table)
local len = #table -- length only has to be determined once as hnext is a closure
local function hnext(key)
local value
key, value = next(table, key)
if type(key) == "number" and key % 1 == 0 and key >= 1 and key <= len then -- list entry, skip
return hnext(key)
end
return key, value
end
return hnext
end
function best_value(table, is_better_func) function best_value(table, is_better_func)
local best_key = next(table) local best_key = next(table)
if best_key == nil then if best_key == nil then