Stiens wrote: ↑Sunday 25 October 2020 15:42
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 ???
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