Hello dzVents users,
I am working on a dzvents script to switchon/switchoff devices depending on the amount of power (watt) that is flowing in or out the grid. Switching off should be done in reversed order than switching on...
Switching on is done with: for device,power in pairs(devices) ....
To switch off i want to iterate the devices list in reversed order. Is there an option in dzVents to accomplish this f.e.
for device,power in pairs(devices).reversed ???
Currently i have a workaround using two lists.
Any suggestions?
Thanks.
Reversed option in for .. in pairs ...
Moderator: leecollings
- waaren
- Posts: 6028
- Joined: Tuesday 03 January 2017 14:18
- Target OS: Linux
- Domoticz version: Beta
- Location: Netherlands
- Contact:
Re: Reversed option in for .. in pairs ...
It depends pretty much on how the table with devices is defined. The method for array like tables is pretty straight forward but for key, value type tables you need two additional steps; first to create a type array helper table and second to sort the array based table on key or value of the original table.
Hope below examples will clarify the principle for you.
Code: Select all
-- -- An array type table can be traversed in order
local leesPlank =
{
"aa p",
"n oo t",
"m ie s",
"w i m",
"z u s",
"j e t",
"t eu n",
"v uu r",
"g ij s",
"l a m",
"k ee s",
"b o k",
"w ei - de",
"d oe s",
"h o k",
"d ui f",
"sch a - p e n",
}
print('\n -- Forward \n' )
for index, word in ipairs(leesPlank) do
print(string.format('%3d',index) .. ': ' .. word)
end
print('\n -- Backwards \n' )
for rIndex = #leesPlank, 1, -1 do -- from size of table down to 1 using stepsize -1
print(string.format('%3d',rIndex) .. ': ' .. leesPlank[rIndex])
end
-- But a key,value type of table does not have a fixed order in Lua
local results =
{
['jan'] = 7,
['jaap'] = 8,
['joris'] = 9,
['corneel'] = 10,
}
print('\n -- unOrdered \n' .. string.format('%11s','Name') .. ': ' .. 'result' ) -- No fixed order; can be different each time
for name, result in pairs(results) do
print(string.format('%11s',name) .. ': ' .. result)
end
-- Need a helper table; sorted
local sorted = {}
for key, value in pairs(results) do
table.insert(sorted, { key,value } ) -- make it an array of key / value records
end
print('\n -- after sorting - Forward based on name \n' .. string.format('%11s','Name') .. ': ' .. 'result' )
table.sort(sorted, function(a, b) return a[1] < b[1] end ) -- sort based on keys of the results table
for index, record in ipairs( sorted ) do
print(string.format('%11s',record[1]) .. ': ' .. record[2]) -- print ordered name, result
end
print('\n -- after sorting - Forward based on result \n' .. string.format('%11s','Name') .. ': ' .. 'result' )
table.sort(sorted, function(a, b) return a[2] < b[2] end ) -- sort based on keys of the results table
for index, record in ipairs( sorted ) do
print(string.format('%11s',record[1]) .. ': ' .. record[2]) -- print ordered name, result
end
print('\n -- after sorting - Backward based on name \n' .. string.format('%11s','Name') .. ': ' .. 'result' )
table.sort(sorted, function(a, b) return a[1] > b[1] end ) -- sort based on keys of the results table
for index, record in ipairs( sorted ) do
print(string.format('%11s',record[1]) .. ': ' .. record[2]) -- print ordered name, result
end
print('\n -- after sorting - Backward based on result \n' ..string.format('%11s','Name') .. ': ' .. 'result' )
table.sort(sorted, function(a, b) return a[2] > b[2] end ) -- sort based on keys of the results table
for index, record in ipairs( sorted ) do
print(string.format('%11s',record[1]) .. ': ' .. record[2]) -- print ordered name, result
end
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
-
- Posts: 2
- Joined: Sunday 25 October 2020 15:17
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Re: Reversed option in for .. in pairs ...
Thx @waaren,
"But a key,value type of table does not have a fixed order in Lua"
This (key,value) is what i am using currently. So the order is not guaranteed.... oeps.
I will study the examples in your answer en redesign my script.
Thanks for pointing me in the right direction,
Lume
"But a key,value type of table does not have a fixed order in Lua"
This (key,value) is what i am using currently. So the order is not guaranteed.... oeps.
I will study the examples in your answer en redesign my script.
Thanks for pointing me in the right direction,
Lume
Who is online
Users browsing this forum: No registered users and 1 guest