lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


hi there! :)

i hope that basically u wrote the same :D

local getAvgWorkerTime=function(log)
  local unpack=unpack or table.unpack
  local sum, count=0, 0
  local workers={}
  for l in io.lines(log) do
    -- hour min sec msec worker message
    local h, m, s, ms, w, msg=l:match'^(%d%d):(%d%d):(%d%d)%.(%d%d%d)
worker%-(%d+).- %- (.*)$'
    -- 1: start; 2: end
    local state=msg and (msg:match'money transfer' and 1 or
msg:match'Transfer to' and 2)
    if state then
      w=tonumber(w)
      local t=tonumber(h)*60*60*1000 + tonumber(m)*60*1000 +
tonumber(s)*1000 + tonumber(ms)
      if state==1 then -- start
        workers[w]=t
      else -- end
        local dt -- delta/diff (as you like it)
        dt=t-workers[w] -- previous
        dt=0<=dt and dt or 60*60*1000-dt -- past midnight
        count=count+1
        sum=sum+dt
        workers[w]=nil end end end -- cleanup, not really necessary
  return math.floor(sum/count).. ' milliseconds' end

Out(getAvgWorkerTime'./tmp/hard.log')


otherwise, where is the challenge?

bests! :)