lua-users home
lua-l archive

Iterating pairs of keys from a table

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I recently had a need to iterate table key combinations in pairs. For example, for a table with keys (not values) of {a, b, c, d}, I wanted to iterate:
 (a,b), (a,c), (a,d), (b,c), (b,d), (c,d)
That is, I wanted unique combinations (not permutations) of non-identical keys. Sort order was not important.
Anyway, I came up with this generator function:
function keypairs(t)
 local s = next(t)
 local n, i = next(t, s), s
 return function()
 i = next(t, i)
 if i == nil then
 s, n = n, next(t, n)
 i = n
 if i == nil then s = nil end
 end
 return s, i
 end
end
Example use:
t = { a=true, b=true, c=true, d=true }
for k1, k2 in keypairs(t) do
 print(k1, k2)
end
However, I was wondering if anyone has a better (faster? smaller? cleaner?) solution they would like to share (or have I missed something very obvious in Lua?). I poked around in the archives and didn’t find anything, but it’s quite possible my limited search missed something.
(The only tweak I thought of was to swap the return order of s and i, which would make the nested “if” unnecessary. However I liked the fact that as written the generator returns the pairs in (major, minor) order that matches that returned by pairs().)
—Tim

AltStyle によって変換されたページ (->オリジナル) /