From 187723118c37f99e5b76de40b54e4cf6bfd2e15c Mon Sep 17 00:00:00 2001 From: Lars Mueller Date: Sun, 10 Jul 2022 12:18:35 +0200 Subject: [PATCH] Add table.hpairs --- table.lua | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/table.lua b/table.lua index 63dc6cb..d1ac960 100644 --- a/table.lua +++ b/table.lua @@ -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