Difference between revisions of "Module:TableTools"
Jump to navigation
Jump to search
add a key/value pair intersection function
en>Mr. Stradivarius (add key/value pair union function) |
en>Mr. Stradivarius (add a key/value pair intersection function) |
||
Line 107: | Line 107: | ||
------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------ | ||
--]] | --]] | ||
function p.intersection(...) | |||
local differentVals = {} -- A substitute variable to use when we have different values for the same key. | |||
local ret, track, pairCounts = {}, {}, {} | |||
local lim = select('#', ...) | |||
for i = 1, lim do | |||
local t = select(i, ...) | |||
for k, v in pairs(t) do | |||
local trackVal = track[k] | |||
if trackVal == nil then | |||
track[k] = v | |||
pairCounts[k] = 1 | |||
elseif trackVal == v then | |||
pairCounts[k] = pairCounts[k] + 1 | |||
end | |||
end | |||
end | |||
for k, v in pairs(track) do | |||
if pairCounts[k] == lim then | |||
ret[k] = v | |||
end | |||
end | |||
return ret | |||
end | |||
--[[ | --[[ |