I'm trying a script that finds the highest value within a table and prints the value's key from the table.
I've tried using math.max combined with unpack() to get this to work. However, unpack() doesn't like the fact I'm not using an array and gives an error saying 'bad argument #1 to 'max' (number expected, got no value)'.
I have no idea how else I'd do this. The script looks a bit useless as I'm simply using it to see if the whole things works, then I'd make it into a script that's a bit more useful.
local test = {
["low"] = 6,
["mid"] = 10,
["high"] = 16
}
function finder()
local top = math.max(unpack(test))
for i,v in pairs (test) do
if v == top then
print(i)
break
end
end
end
finder()
So if it was working, it should just print "high" to the output. Sorry if I've worded things a bit wrong, I'm trying to make it as understandable as I can.

1 comment
Thanks for the quick answer! :)