Module:Demographics Basics

require "Module:FormatNum" require "Module:OrderedPairs" require "Module:SortedKeys"

function total_table(table,country) local temp = 0 if table == nil then return 0 elseif table[country] == nil then return 0 elseif table[country] == 0 then return 0 else for i in pairs(table[country]) do temp=temp+table[country][i] end return temp end end

function fetch_table(table,country,race) if table == nil then return 0 elseif table[country] == nil then return 0 elseif table[country] == 0 then return 0 end if table[country][race] == nil then return 0 elseif table[country][race] == 0 then return 0 else return table[country][race] end end

function compare(a,b) return a < b end

function generate(tabule,country,type) local totalled=total_table(tabule,country) local Base= "{| class=\"sortable wikitable\" style=\"background:white;\"\n! style=\"text-align: center; font-weight: bold;\" | " .. type .."\n! style=\"text-align: center; font-weight: bold;\" | Amount\n! style=\"text-align: center; font-weight: bold;\" | Percentage\n" table.sort(tabule,compare) for key,value in pairs(tabule[country]) do       if tabule[country][key] ~= nil and tabule[country][key] ~= 0 then if key ~= "O" and key ~= "Other" then Base = Base .. "|-\n| style=\"text-align: center;\" | " .. key .. "\n" else Base = Base .. "|-\n| style=\"text-align: center;\" | " .. "Other" .. "\n" end Base = Base .. "| style=\"text-align: center;\" | " .. format_num(value,0) .. "\n" Base = Base .. "| style=\"text-align: center;\" | " .. (math.ceil(value/totalled*10000))/100 .. "%\n" end end Base = Base .. "|}"   return Base end

function generate_historical(tabule,country,type) local Origin = {} local i = 1 for key,value in orderedPairs(tabule[country]) do		Origin[i] = tabule[country][key] i = i + 1 end local Base= "{| class=\"wikitable\"\n! style=\"text-align: center; font-weight: bold;\" | Year\n! style=\"text-align: center; font-weight: bold;\" | Pop\n! style=\"text-align: center; font-weight: bold;\" | ±%\n! style=\"text-align: center; font-weight: bold;\" | Notes\n" for key,value in pairs(Origin) do		if Origin[key] ~= nil and Origin[key] ~= 0 then Base = Base .. "| style=\"text-align: center; font-weight: bold;\" |" .. FormatNum(Origin[key][1]) .. "\n" if key == 1 then Base = Base .. "| style=\"text-align: center; font-weight: bold;\" |" .. "-" .. "\n" else Base = Base .. "| style=\"text-align: center; font-weight: bold;\" |" .. Compare(Origin[key][1],Origin[key-1][1]) .. "\n" end if Origin[key][2] ~= nil and Origin[key][2] ~= 0 then Base = Base .. "| style=\"text-align: center; font-weight: bold;\" |" .. Origin[key][2] .. "\n" else Base = Base .. "| style=\"text-align: center; font-weight: bold;\" | \n" Base = Base .. "|-\n" end end end Base = Base .. "|}"	return Base end