Difference between revisions of "Module:Demographics Basics"

From Ways of Darkness
Jump to navigation Jump to search
Line 35: Line 35:
  
 
function generate(table,country,type)
 
function generate(table,country,type)
     local totalled=totaltable(table,country)
+
     local totalled=total_table(table,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"
 
     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"
 
     for key,value in orderedPairs(table[country]) do
 
     for key,value in orderedPairs(table[country]) do

Revision as of 21:06, 23 February 2016

Documentation for this module may be created at Module:Demographics Basics/doc

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

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 generate(table,country,type)
    local totalled=total_table(table,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"
    for key,value in orderedPairs(table[country]) do
        if table[country][key] ~= nil and table[country][key] ~= 0 then
        Base = Base .. "|-\n| style=\"text-align: center;\" | [[" .. key .. "]]\n"
        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