Reputation: 91
I wrote a Lua code to arrange a list but when I enter any number with 2 decimals as 10, 20, etc. in the list, the variable 'ordenado' always takes the value 1 although whether it fulfills the conditions or not.
valor = {}
ordenado = 0
function inicializar ()
for i = 1,10 do
print ("Introduzca el valor "..i..":")
valor[i] = io.read()
end
end
function verificar ()
for i = 2, #valor do
if valor[i]>valor[i-1] then
ordenado = ordenado + 0
else
ordenado = ordenado + 1
end
print ("actual: "..valor[i].." \nanterior: "..valor[i-1].."\nordenado:"..ordenado.."\n")
end
end
function imprimir()
if ordenado == 0 then
print "La lista esta ordenada"
else
print "La lista no esta ordenada"
end
end
a = inicializar()
a = verificar()
a = imprimir()
The Lua version is 5.2.
Upvotes: 1
Views: 203
Reputation: 72312
The line valor[i] = io.read()
stores a string in valor[i]
. As strings, "2" > "10"
.
Upvotes: 1