Module:TableUtil

-- TableUtil local p = {}

function p.keyOf(tbl, val) for k, v in pairs(tbl) do		if v == val then return k		end end return nil end

function p.hash(tbl) local hash = {} for k, v in pairs(tbl) do		hash[v] = true end return hash end

-- sorts tblToSort to be in the same order as the elements appear in lookup function p.sortByKeyOrder(tblToSort,lookup) table.sort(tblToSort, function (a,b)			return (p.keyOf(lookup,a) or 0) < (p.keyOf(lookup,b) or 0)		end	) return end

function p.mergeArrays(tbl1,tbl2) -- tbl1 is modified to include the elements of tbl2 appended to the end. Order is preserved. for _, v in ipairs(tbl2) do		tbl1[#tbl1+1] = v	end return end

-- table.remove for non-integer key function p.remove(tbl, key) local output = tbl[key] tbl[key] = nil return output end

-- returns a copy of tbl with the elements in opposite order (not a deep copy) function p.reverse(tbl) local tbl2 = {} local len = #tbl for i = len, 1, -1 do		tbl2[len - i + 1] = tbl[i] end return tbl2 end

return p