Add table.hpairs

master
Lars Mueller 2022-07-10 12:18:35 +02:00
parent d4c6468cec
commit 187723118c
1 changed files with 14 additions and 0 deletions

View File

@ -616,6 +616,20 @@ function rpairs(table)
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)
local best_key = next(table)
if best_key == nil then