Lua: slice table


Code snippet:

local unpack = unpack or table.unpack

table.slice = function(a, start ,_end)
	return {unpack(a, start, _end)}
end

Example:

local a = {1, 2, 3, 4, 5, 6, 7, 8, 9}

for key, value in pairs(table.slice(a,2,3)) do
    print(key, ":", value)
end

Result:

1:2
2:3

Online demo

Leave a Reply