Module: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.lookup(tbl) local lookup = {} for k, v in pairs(tbl) do		lookup[v] = k	end return lookup end

function p.appendLookup(tbl, parent) for k, v in pairs(tbl) do		parent[v] = k	end return end

-- sorts tblToSort to be in the same order as the elements appear in lookup function p.sortByKeyOrder(tblToSort,values) local lookup = p.lookup(values) table.sort(tblToSort, function (a,b)			return (lookup[a] or 0) < (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

function p.merge(tbl1, tbl2) -- tbl1 is modified to include all the elements of tbl2. for k, v in pairs(tbl2) do		tbl1[k] = 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

function p.slice(tbl, s, e)	local tbl2 = {} for k = s, e do		tbl2[#tbl2+1] = tbl[k] end return tbl2 end

-- prints the table as a comma-separated list with and function p.printList(tbl) if #tbl == 1 then return tbl[1] elseif #tbl == 2 then return table.concat(tbl, ' and ') else last = table.remove(tbl, #tbl) list = table.concat(tbl, ', ') return list .. ', and ' .. (last or '') end end

function p.removeFalseEntries(tbl) for i = #tbl, 1, -1 do		if not tbl[i] then table.remove(tbl, i)		end end return tbl end

function p.concatSpecial(tbl, sep, f, f_args) -- f is some formatting function to apply to all non-empty entries in the table -- f_args is a set of optional additional args to send to f	-- requires that the processing function uses processArgs p.removeFalseEntries(tbl) if f then if not f_args then f_args = {} end for k, v in ipairs(tbl) do			f_args[1] = v			tbl[k] = f(f_args) end end return table.concat(tbl, sep) end

function p.concatFromArgsSpecial(args, argname, sep, f, f_args) -- if fields are saved in args as field1, field2, field3, etc local i = 1 local tbl = {} if args[argname] then tbl[1] = args[argname] i = 2 end while args[argname .. i] do tbl[i] = args[argname .. i]		i = i + 1 end return p.concatSpecial(tbl, sep, f, f_args) end

function p.concat(tbl, sep, f) -- f is a function that doesn't take any additional args p.removeFalseEntries(tbl) if f then for k, v in ipairs(tbl) do			tbl[k] = f(v) end end return table.concat(tbl, sep) end

function p.concatFromArgs(args, argname, sep, f)	-- if fields are saved in args as field1, field2, field3, etc local i = 1 local tbl = {} if args[argname] then tbl[1] = args[argname] i = 2 end while args[argname .. i] do tbl[i] = args[argname .. i]		i = i + 1 end return p.concat(tbl, sep, f) end

return p