Module:Demographics/PopQuery

local p = { }

function p.filter(value,fltable) if fltable == nil then return true end if fltable == 0 then return true end if fltable == "0" then return true end if next(fltable) == nil then return true end local match = false for _,v in pairs(fltable) do		if value == v then match = true end end return match end

function p.query(groupBy, byValue, filterRace, filterReligion, filterOccupation, filterAgegroup, filterGender, filterCountry, filterRegion, filterArea) local pops = mw.loadData( 'Module:Demographics/Pops' ) local queried = { } for popId,popTable in pairs(pops) do		if filter(popTable['race']['raceName'],filterRace) and filter(popTable['religion']['religionName'],filterReligion) and filter(popTable['occupation']['occupationName'],filterOccupation) and filter(popTable['agegroup']['agegrpName'],filterAgegroup) and filter(popTable['area']['region']['country']['countryName'],filterCountry) and filter(popTable['area']['region']['regionName'],filterRegion) and filter(popTable['area']['areaName'],filterArea) then if queried[popTable['groupBy']['byValue']] == nil then queried[popTable['groupBy']['byValue']] = popTable['quantity'] else queried[popTable['groupBy']['byValue']] = queried[popTable['groupBy']['byValue']] + popTable['quantity'] end end end end

return p