Module:Chaine

Une page de Wikiversité, la communauté pédagogique libre.

Ce module sert d'exemple dans la leçon Initiation au Lua avec Scribunto. son utilisation est décrite en détail dans celle-ci et, par conséquent, ce module ne doit pas être modifié sans tenir compte de la leçon.


local p = {}

function p.visuString(frame)
	reponse = ""
	for index, objet in pairs(string) do
		reponse = reponse.."<br />À la clé '''"..index.."''', on trouve un objet de type : "..type(objet)
	end
	return reponse
end

function p.visuUstring(frame)
	reponse = ""
	for index, objet in pairs(mw.ustring) do
		reponse = reponse.."<br />À la clé '''"..index.."''', on trouve un objet de type : "..type(objet)
	end
	return reponse
end

function p.visuText(frame)
	reponse = ""
	for index, objet in pairs(mw.text) do
		reponse = reponse.."<br />À la clé '''"..index.."''', on trouve un objet de type : "..type(objet)
	end
	return reponse
end

function p.capitale(frame)
	local mot = frame.args[1]
	return string.upper(mot)
end

function p.minuscule(frame)
	local mot = frame.args[1]
	return string.lower(mot)
end

function p.palindrome(frame)
	local mot = frame.args[1]
	if mot == string.reverse(mot) then
		return mot.." est un palindrome."
	else
		return mot.." n’est pas un palindrome."
	end
end

function p.echo(frame)
	local mot = frame.args[1]
	local nombre = tonumber(frame.args[2])
	return string.rep(mot,nombre)
end

function p.longueur(frame)
	local phrase = frame.args[1]
	return string.len(phrase)
end

function p.extrait(frame)
	phrase = frame.args[1]
	debut = tonumber(frame.args[2])
	fin = tonumber(frame.args[3])
	return string.sub(phrase,debut,fin)
end

function p.oeuf()
	local phrase,nombre = string.gsub("Je casse un œuf dur", "casse", "mange")
	return phrase.."(Le nombre de mots remplacés est "..nombre..")"
end

function p.cherche(frame)
	local mot = frame.args[1]
	local phrase = "Rien ne sert de courir,il faut partir à point"
	local position = string.find(phrase,mot)
	if position then
		return "Le mot recherché se trouve à la position : "..position
	else
		return "Je n'ai pas trouvé !"
	end
end

function p.formatage()
	local reponse = " "
	reponse = reponse.."<br />Avec c, on obtent : "..string.format('%c',87).." et "..string.format('%c',36)
	reponse = reponse.."<br />Avec d, on obtent : "..string.format('%d',56.235).." et "..string.format('%d',-23.827)
	reponse = reponse.."<br />Avec E, on obtent : "..string.format('%E',0.097).." et "..string.format('%E',-2833.24)
	reponse = reponse.."<br />Avec e, on obtent : "..string.format('%e',0.097).." et "..string.format('%e',-2833.24)
	reponse = reponse.."<br />Avec f, on obtent : "..string.format('%f',0.097).." et "..string.format('%f',823.43657835)
	reponse = reponse.."<br />Avec g, on obtent : "..string.format('%g',1.5789235E4).." et "..string.format('%g',-1.5789235e-4)
	reponse = reponse.."<br />Avec G, on obtent : "..string.format('%G',1.5789235E4).." et "..string.format('%G',-1.5789235e-4)
	reponse = reponse.."<br />Avec i, on obtent : "..string.format('%i',56.235).." et "..string.format('%i',-23.827)
	reponse = reponse.."<br />Avec o, on obtent : "..string.format('%o',9).." et "..string.format('%o',93)
	reponse = reponse.."<br />Avec u, on obtent : "..string.format('%u',-1).." et "..string.format('%u',21.2479)
	reponse = reponse.."<br />Avec X, on obtent : "..string.format('%X',47).." et "..string.format('%X',28)
	reponse = reponse.."<br />Avec x, on obtent : "..string.format('%x',169).." et "..string.format('%x',735)
	reponse = reponse.."<br />Avec q, on obtent : "..string.format('%q',"La voiture roule")
	reponse = reponse.."<br />Avec s, on obtent : "..string.format('%s',"La voiture roule")
	return reponse
end
	

return p