Flaghacker

custom iterator

Jul 12th, 2014
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.04 KB | None | 0 0
  1. local t =
  2.   {
  3.     one =
  4.       {
  5.         state = "storage",
  6.         locat = {station = 1, row = 5, collum = 3}
  7.         prior = 3,
  8.         descr = "This is description 1!"
  9.       },
  10.     two =
  11.       {
  12.         state = "storage",
  13.         locat = {station = 1, row = 5, collum = 4}
  14.         prior = 1,
  15.         descr = "This is description 2!"
  16.       },
  17.     three =
  18.       {
  19.         state = "track",
  20.         locat = {station = 1, row = 5, collum = 5}
  21.         prior = 2,
  22.         descr = "This is description 3!"
  23.       }
  24.   }
  25.  
  26. local function storage(t)
  27.   local curr = 0
  28.   local toDo = {}
  29.   --populate toDo table
  30.   for i, j in pairs(t) do
  31.     if j["state"] == "storage" then
  32.       toDo[#toDo + 1] = i
  33.     end
  34.   end
  35.   table.sort(toDo)
  36.   --iterator stuff
  37.   return function()
  38.     curr = curr + 1
  39.     --stop condition
  40.     if curr <= #toDo then
  41.       --for loop parameters return
  42.       return toDo[curr], t[toDo[curr]]
  43.     else
  44.       return nil
  45.   end
  46.   end
  47. end
  48.  
  49. for name, data in storage(t) do
  50.   print(name..": "..data["descr"])
  51. end
Advertisement
Add Comment
Please, Sign In to add comment