Files
2022-03-24 14:57:19 +01:00

99 lines
2.6 KiB
Lua

--[[---------------------------------------------------
Procedura calcolo statistiche parametri:
Variabili IN:
- vcFunct(string): tipo di processing da effettuare tra [POINT/AVG/MEDIAN/MIN/MAX]
- valList(<double>): elenco VALORI double da processare
- numRec(int): conteggio numero recorda da processare
Variabili OUT:
- calcVal(double): valore calcolato finale
-----------------------------------------------------]]
-- Per eventuale debug
local ZBS = "c:/ZeroBraneStudio"
if not package.path:find(ZBS,1,true) then
package.path = ZBS .. "/lualibs/?/?.lua;" .. ZBS .. "/lualibs/?.lua;" .. package.path
package.cpath = ZBS .. "/bin/?.dll;" .. ZBS .. "/bin/clibs53/?.dll;" .. package.cpath
end
-- se non è da NLua inizializzo variabili accessorie
local function checkInit()
if (callMode ~= 'NLua') then
-- imposto valori test
valList = { 4467, 4468, 4467, 4467, 4467, 4467 }
--valList = { 4.0, 5.0, 3.0, 6.0, 1.0, 2.0 }
vcFunct = 'AVG'
--POINT AVG MEDIAN MIN MAX
end
end
local function setupTable()
if numRec > 0 and valList[0] ~= nil then
for i = numRec, 1, -1 do
valListTable[i] = valList[i-1]
end
else
valListTable = valList
end
end
local function doCalc()
if(numRec > 0) then
-- verifica il tipo di richiesta
if(vcFunct == 'AVG') then
s = 0
for i,v in ipairs(valListTable) do
s = s + v
end
calcVal = s / numRec
elseif(vcFunct == 'POINT') then
calcVal = valListTable[numRec]
elseif(vcFunct == 'MEDIAN') then
table.sort(valListTable)
calcVal = valListTable[numRec/2]
elseif(vcFunct == 'MIN') then
table.sort(valListTable)
calcVal = valListTable[1]
elseif(vcFunct == 'MAX') then
table.sort(valListTable)
calcVal = valListTable[numRec]
end
calcOk = true
else
calcVal = -999999
numRec = 1
calcOk = false
end
end
local function displayTestInfo()
if (callMode ~= 'NLua') then
print('------------------------------')
print('calcOk: ' .. tostring(calcOk) .. ' | vcFunct: ' .. vcFunct)
for i,val in pairs(valList) do
print("v_"..i.." | "..val)
end
print('calcVal: ' .. calcVal)
print('------------------------------')
end
end
-- MAIN
-- variabile semaforo callMode (locali o remote da NLua)
callMode = callMode or ''
calcVal = -999
vcFunct = vcFunct or ''
valList = valList or {}
numRec = numRec or 1
calcOk = false
valListTable = {}
checkInit()
setupTable()
doCalc()
displayTestInfo()