lua table find higest value - Printable Version +- Logic Machine Forum (https://forum.logicmachine.net) +-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1) +--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8) +--- Thread: lua table find higest value (/showthread.php?tid=2695) |
lua table find higest value - benanderson_475 - 23.06.2020 I want to get the highest time and then switch the groups on in the order from highest time(stored in on_time) to lowest time until all the groups are on, groups/ range i am interested in are 1/1/1 to 1/1/20 i am storing the obj.updatetime in a table with the associated address etc as below Code: function chk_off() Then i want to get the highest value from all the t.on_time/s and switch each off, in order from the longest off value (stored in on_time) until all the complete range 1/1/1 - 1/1/20 are all off this runs from a function which is called from a scheduled script every x minutes. This is where i am stuck, this is what i have so far, my question is how can i calculate the highest on_time value and what will happen if two possibility have the same on_time value? I hope this is making sense.... Code: function load_time_off() RE: lua table find higest value - admin - 23.06.2020 You can find the maximum value like this. If two items have the same on_time then the one that appears first in the table will be returned. Code: max_index = 0 Or you can sort the table using custom comparison function. In this case the order of elements in not defined if on_time is the same. t[1] will contain the element with maximum on_time value. Code: table.sort(t, function(a, b) RE: lua table find higest value - benanderson_475 - 23.06.2020 (23.06.2020, 06:28)admin Wrote: You can find the maximum value like this. If two items have the same on_time then the one that appears first in the table will be returned.Many Thanks, i have it working |