Dokumentationen för denna modul kan skapas på Modul:Stat/Sverige/dok

local p = {}
 
function p.SCB(frame)
    local orttyp  = frame.args[1] or "" -- "Tätorter" eller "Småorter"
    local stattyp = frame.args[2] or "" -- "Areal", "Befolkning" eller "Täthet"
    local ortkod  = frame.args[3] or "" -- T.ex. "1206" eller "S0105"
    local year    = frame.args[4] or "" -- T.ex. "2010"
 
	local res = "saknas"
	
    ortkod = ortkod:gsub("Å", "A"):gsub("Ä", "A"):gsub("Ö", "O"):gsub("å","a"):gsub("ä","a"):gsub("ö","o"):gsub(" ","")

	if( orttyp ~= "Tätorter" and orttyp ~= "Småorter" ) then
		if ( string.sub(ortkod,1,1) == "S" ) then
			orttyp = "Småorter"
		else
			orttyp = "Tätorter"
		end
	end
 
 	if( ( orttyp == "Tätorter" ) and ( string.sub(ortkod,1,1) ~= "T" ) ) then
		ortkod = "T" .. ortkod
	end
	if( stattyp == "Täthet" ) then
        local areal      = mw.loadData( "Module:Stat/Sverige/" .. orttyp .. "/Areal/" .. year )
        local befolkning = mw.loadData( "Module:Stat/Sverige/" .. orttyp .. "/Befolkning/" .. year )
        local folk = befolkning.value [ ortkod ]
        local yta  = areal.value [ ortkod ]
        if( folk and yta ) then
        	res = math.floor( ( 100 * folk  / yta ) + 0.5 )
        end
	elseif ( stattyp == "Areal" ) then
		local tab = mw.loadData( "Module:Stat/Sverige/" .. orttyp .. "/" .. stattyp .. "/" .. year )
		res = tab.value[ ortkod ] or res
	elseif ( stattyp == "Befolkning" ) then
		local tab = mw.loadData( "Module:Stat/Sverige/" .. orttyp .. "/" .. stattyp .. "/" .. year )
		res = tab.value[ ortkod ] or res
	elseif ( stattyp == "Kod" ) then
		res = ortkod
	end
	return res
end
 
return p