Logic Machine Forum
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()
  t ={}

for i = 1, 20 do
    addr = '1/1/'.. tostring(i)
    obj = grp.find(addr)
   
  if obj and obj.value == 0  then
     --put all off obj in table with update time
      table.insert(t, {address = addr, level = obj.value, upd_time = obj.updatetime, on_time = os.time() -obj.updatetime }  )
    end     
end
  return t
end 

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()
t = chk_off()
if t then  
for k, v in ipairs(t) do
log(v.on_time) -- this gives me all the on_time vals
-- how to get highest on_time value?? and switch on the associated address
   end
end    
end



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
max_time = 0

for index, item in ipairs(t) do
  if item.on_time > max_time then
    max_index = index
    max_time = item.on_time
  end
end

max_item = t[ max_index ]
if max_item then
  log(max_item)
end

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)
  return a.on_time > b.on_time
end)

log(t)



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.
Code:
max_index = 0
max_time = 0

for index, item in ipairs(t) do
  if item.on_time > max_time then
    max_index = index
    max_time = item.on_time
  end
end

max_item = t[ max_index ]
if max_item then
  log(max_item)
end

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)
  return a.on_time > b.on_time
end)

log(t)
Many Thanks, i have it working