fork(35) download
  1. function table.slice(tbl, first, last, step)
  2. local sliced = {}
  3.  
  4. for i = first or 1, last or #tbl, step or 1 do
  5. sliced[#sliced+1] = tbl[i]
  6. end
  7.  
  8. return sliced
  9. end
  10.  
  11. local a = {1, 2, 3, 4}
  12. local b = table.slice(a, 2, 3)
  13. print(a[1], a[2], a[3], a[4])
  14. print(b[1], b[2], b[3], b[4])
Success #stdin #stdout 0.02s 2496KB
stdin
Standard input is empty
stdout
1	2	3	4
2	3	nil	nil