Page 2 of 2

Re: Finding the minimum value... (not math.min)

Posted: Thu Jan 14, 2010 6:46 pm
by Robin
bartbes's is better.

Re: Finding the minimum value... (not math.min)

Posted: Thu Jan 14, 2010 7:27 pm
by napco
Or, again:

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
I LOVE programming because it's almost impossible to have only one solution to a specific problem!

Re: Finding the minimum value... (not math.min)

Posted: Sun Feb 07, 2010 8:58 am
by appleide

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
Who needs for loops 8-)