FichteFoll

Lua - numeric_table

Jan 29th, 2012
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.65 KB | None | 0 0
  1. function validation(subs,sel)
  2.   if aegisub.frame_from_ms() then
  3.     return true, "True"
  4.   else
  5.     return false, "False"
  6.   end
  7. end
  8.  
  9. function nothing()
  10.     return numeric_table({26, 28, "30-33"}, "37", {_G, {38, "-23--2"}}) -- select a few lines
  11. --~     return nil
  12. end
  13.  
  14. function numeric_table(...)
  15.     -- Values that are going to be returned
  16.     local values = {}
  17.     -- Table that holds already parsed tables' references for avoiding infinite loops
  18.     local circular = {}
  19.  
  20.     local function resolve_recursive(t, callback)
  21.         if not type(t) == "table" then return nil; end
  22.  
  23.         circular[t] = true
  24.         -- Iterate over all values
  25.         for _, v in pairs(t) do
  26.             if type(v) == "table" then
  27.                 -- Value is another table, recursive call
  28.                 -- But only if it has not already been parsed, since we have those values already
  29.                 if not circular[v] then
  30.                     resolve_recursive(v, callback)
  31.                 end
  32.             else
  33.                 callback(v)
  34.             end
  35.         end
  36.     end
  37.     local function parse_value(v)
  38.         -- Check the value for different types and interpret it
  39.         if type(v) == "number" then
  40.             -- Just a number
  41.             table.insert(values, v)
  42.         elseif type(v) == "string" then
  43.             local low, high = v:match("^(-?%d+)-(-?%d+)$")
  44.             -- A range of numbers
  45.             if low and high then
  46.                 for low = low, high do
  47.                     table.insert(values, low)
  48.                 end
  49.             -- The string is convertable
  50.             elseif tonumber(v) then
  51.                 table.insert(values, tonumber(v))
  52.             end
  53.         end
  54.     end
  55.  
  56.     -- Iteterate over all params
  57.     resolve_recursive({...}, parse_value)
  58.     for i, v in ipairs(values) do
  59.         aegisub.log(string.format("values[%d] = %d\n", i, v))
  60.     end
  61.     return values or nil
  62. end
  63.  
  64. aegisub.register_macro("Test","True",nothing,validation)
Advertisement
Add Comment
Please, Sign In to add comment