Aller au contenu

Module:Bac à sable/Utilisateur:GrandEscogriffe

Une page de Wikipédia, l'encyclopédie libre.

 Documentation[créer] [purger]
local p = {}

function p.showdata(frame)
	--local args1 = frame:getParent().args
	--local args2 = frame.args
	
	local selectname, selectvalue, resultname, resultvalue, selectindex, resultindex
	selectname = 'date'
	selectvalue = '1870-01'
	resultname = 'snowfall'
	filename = 'Ncei.noaa.gov/weather/New York City.tab'
	
	local datatable = mw.ext.data.get(filename)
	
	for j,field in ipairs(datatable.schema.fields) do
		if field.name == selectname then
			if selectindex then error('Table has several columns with the selection name') end
			selectindex = j
		end
		if field.name == resultname then
			if resultindex then error('Table has several columns with the result name') end
			resultindex = j
		end
	end
	
	for i,row in ipairs(datatable.data) do
		if tostring(row[selectindex]) == selectvalue then
			if resultvalue then error('Table has several rows with the selection value') end
			resultvalue = tostring(row[resultindex])
		end
	end
	
	local result = resultvalue
	
	if type(result) == 'string' then return result end
	if type(result) == 'number' then return tostring(result) end
	if type(result) == 'table' then
		local start = true
		local str = '{'
		for i,j in pairs(result) do
			if type(j) == 'table' then j = 'table' end
			if not start then str = str .. ', ' end
			str = str .. i .. ' = ' .. j
			start = false
		end
		str = str .. '}'
		return str
	end
end
 
return p