Получение части массива

Массивы и коллекции / Массивы

local numbers = {2, 3, 5, 7, 11}

-- The range 2..4 includes both ends
local middle = table.move(numbers, 2, 4, 1, {})
-- middle = 3, 5, 7

local first2 = table.move(numbers, 1, 2, 1, {})
-- first2 = 2, 3

-- Count from the end by hand
local from = #numbers - 1
local last2 = table.move(numbers, from, #numbers,
                         1, {})
-- last2 = 7, 11

-- unpack gives the items as separate values
local x, y = table.unpack(numbers, 4, 5)
-- x = 7
-- y = 11

-- A slice is a copy, not a view
numbers[2] = 50
-- middle stays 3, 5, 7

print("middle = " .. table.concat(middle, ", "))
print("first2 = " .. table.concat(first2, ", "))
print("last2 = " .. table.concat(last2, ", "))
print("x = " .. x)
print("y = " .. y)