Re: Finding the minimum value... (not math.min)
Posted: Thu Jan 14, 2010 6:46 pm
bartbes's is better.
Code: Select all
function get_min(t)
min = math.min(unpack(t))
for i, v in ipairs(t) do
if v == min then
return v, i
end
end
end
Code: Select all
function min_index(first, ...)
return math.min(first, ...) == first and 1 or min_index(...) + 1
end
function test(...)
print(min_index(...))
end
test(1, 2, 3, 4) -- prints 1
test(4, 3, 1, 5, 6, 7) -- prints 3