How would I compare all table values?

For example, I have a table: {10, 20, 30, 40, 50}

All I want to do is find out which value is the biggest one out of all the values.

Simple, right? WRONG. I couldn’t figure it out for a good hour and a half.

1 Like

Not sure if there is a better way, there probably is, but:

You can have a variable called “highest” or something, loop through the table and if the current value is greater than the highest value then set highest to the current value.

2 Likes

It’s so simple! it really is…

@RudraVII yes , there is a better way

This is a much more explanatory solution (and actually solves the problem)

Do this, utilize math.max

 local tablex = {10, 20, 30, 40, 50}
   print(math.max(table.unpack(tablex)))

Output

prints --------> 50 !

we use table.unpack to basically get all the components in an array, then use math.max to compare them to find the highest value, you could use math.min to get the lowest.

5 Likes

Thank you, I learned something off this, too. Will be useful for the future.

1 Like

Thank you for this! It does not fit my situation, but its a thing i have been looking for a while.