A collection of various things from the Asterisk server
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

parse_table.lua 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. local function read_table(filename, file_table, errout)
  2. local function parse_line(line, file_table, errout)
  3. local s, e = string.find(line, " : ")
  4. if s == nil or e == nil then
  5. errout("error: bad line")
  6. return
  7. end
  8. local cum = tonumber(string.sub(line, 1, s - 1))
  9. local file = string.sub(line, e + 1)
  10. table.insert(file_table, { cum = cum, file = file })
  11. end
  12. local tabf = io.open(filename, "r")
  13. if tabf == nil then
  14. errout("could not open table file " .. filename)
  15. return
  16. end
  17. for line in tabf:lines() do
  18. parse_line(line, file_table, errout)
  19. end
  20. io.close(tabf)
  21. end
  22. local function search_table(file_table, cum)
  23. local s = 1
  24. local e = #file_table - 1
  25. while true do
  26. if s >= e then
  27. return s
  28. end
  29. local sidx = math.floor((s + e) / 2)
  30. if cum < file_table[sidx].cum then
  31. e = sidx
  32. else
  33. s = sidx + 1
  34. end
  35. end
  36. end
  37. local ftab = {}
  38. read_table("outtab.txt", ftab, function(em) print(em) end)
  39. --for idx, ent in ipairs(ftab) do
  40. -- print(tostring(idx) .. " cwgt(" .. tostring(ent.file) .. ") = " .. tostring(ent.cum))
  41. --end
  42. local uw = ftab[#ftab - 1].cum
  43. math.randomseed(os.time())
  44. for i=1,100 do
  45. local eidx = search_table(ftab, math.random() * uw)
  46. local fname = ftab[eidx].file
  47. print(fname)
  48. end
  49. return { read_table = read_table, search_table = search_table }