vlatkovski

copy

Jun 9th, 2015
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.79 KB | None | 0 0
  1. function copy(table1, table2, positionFunction)
  2.     if not table1 then return end
  3.  
  4.     local table3 = table2 or {}
  5.  
  6.     positionFunction = positionFunction or function() return true end
  7.     local position = 0
  8.  
  9.     for index, value in next, table1 do
  10.         position = position + 1
  11.         if (positionFunction and positionFunction(index, value, position)) then
  12.             table3[index] = value
  13.         end
  14.     end
  15.  
  16.     return table3
  17. end
  18.  
  19. ------------------------------------------------------------------
  20.  
  21. local example = {[1]="a", [2]="b", [3]="c", [4]="d", [5]="e", [6]="f"}
  22.  
  23. local copied = copy(example, {}--[[, function(index, value, position)
  24.     return position <= 4 --the result will be to copy only the first 4 things in "example" into an empty table
  25. end]])
  26.  
  27.  
  28. for i,v in next, copied do print(i,v) end --print out results
Advertisement
Add Comment
Please, Sign In to add comment