11.02.2021, 14:43
(11.02.2021, 14:28)admin Wrote: Use arrays/tables to store data like this. Untested modified version of you code:
Code:obj = db:getall('SELECT name FROM objects WHERE name LIKE "%STATUS_OCCUPIED%"') data = {} keys = { '02', '03_04', '05' } for fid = 1, 5 do floor = {} for _, key in ipairs(keys) do floor[key] = { count = 0, occ = 0, } end data[fid] = floor end for _, name in ipairs(obj) do adr = grp.find(name.name) gaWeight = string.gsub(name.name, 'STATUS', 'WEIGHT') weight = grp.getvalue(gaWeight) value = toboolean(grp.getvalue(name.name)) floor = tonumber(string.split(adr.address, '/')[2]) agg = string.split(string.split(adr.name, '360.')[2], ' ')[1] --Oppretter adresse for vekting dersom den ikke finnes og setter vekttallet til 1. if grp.find(gaWeight) == nil then address = grp.create({ datatype = dt.uint16, virtual = true, name = gaWeight, comment = '', }) grp.write(gaWeight, 1) weight = 1 end if data[floor] and data[floor][agg] then data[floor][agg].count = data[floor][agg].count + 1 if value then data[floor][agg].occ = data[floor][agg].occ + weight end end end log(data)
Great!
I will test this out. It looks a lot more elegant than my solution. I believe I will be needing things like this in many other applications as well.
There are 10 kinds of people in the world; those who can read binary and those who don't