diff options
Diffstat (limited to 'lualibs-table.lua')
-rw-r--r-- | lualibs-table.lua | 34 |
1 files changed, 30 insertions, 4 deletions
diff --git a/lualibs-table.lua b/lualibs-table.lua index 11cb66b..c318c57 100644 --- a/lualibs-table.lua +++ b/lualibs-table.lua @@ -129,10 +129,13 @@ local function sortedhash(t,cmp) s = sortedkeys(t) -- the robust one end local n = 0 + local m = #s local function kv(s) - n = n + 1 - local k = s[n] - return k, t[k] + if n < m then + n = n + 1 + local k = s[n] + return k, t[k] + end end return kv, s else @@ -1003,7 +1006,9 @@ function table.print(t,...) end end -setinspector(function(v) if type(v) == "table" then serialize(print,v,"table") return true end end) +if setinspector then + setinspector(function(v) if type(v) == "table" then serialize(print,v,"table") return true end end) +end -- -- -- obsolete but we keep them for a while and might comment them later -- -- -- @@ -1054,3 +1059,24 @@ function table.sorted(t,...) sort(t,...) return t -- still sorts in-place end + +-- + +function table.values(t,s) -- optional sort flag + if t then + local values, keys, v = { }, { }, 0 + for key, value in next, t do + if not keys[value] then + v = v + 1 + values[v] = value + keys[k] = key + end + end + if s then + sort(values) + end + return values + else + return { } + end +end |