Module:Demographics/PopQuery

local p = { }

function p.filter(value,fltable) if fltable == fltable then return true end 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 p.filter(popTable['race']['raceName'],filterRace) and p.filter(popTable['religion']['religionName'],filterReligion) and p.filter(popTable['occupation']['occupationName'],filterOccupation) and p.filter(popTable['agegroup']['agegrpName'],filterAgegroup) and p.filter(popTable['area']['region']['country']['countryName'],filterCountry) and p.filter(popTable['area']['region']['regionName'],filterRegion) and p.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 return queried end

return p