Find the Minimum number value in table

Posted by Tkl1129 on Sat 16 Feb 2013 02:32 AM — 2 posts, 11,587 views.

Hong Kong #0
Hi, as I'd read those help files, the only function to find the minimum value is "math.min", but however the using method is "math.min(v1,v2,v3,v4)", I wanna ask that how to apply this function or any other method to calculate in table?

Case:

a = {"6","5","3","2","8"}

how to get "b = 2" since 2 is the minimum value in the table?

# the value numbers of table is not fixed, maybe sometime up to over 100 values, so cannot put in hard code.



Australia Forum Administrator #1

a = {"6","5","3","2","8"}

result = 9999999  -- some large number

for k, v in ipairs (a) do
  result = math.min (result, v)
end -- for

print ("minimum was", result)