Is there a way to split a table or get a sub-table from a table without having to iterate over it?

Is there any way to split a table or get a sub-table from a table, without having to iterate over the table and adding each item into a new table?

For example
t = {1, 2, 3, 4, 5}
is there a way to return
{1, 2}
or
{1, 2, 3, 4}` ?

If anyone knows Python it’d be something like
t[:2]
which returns the items from the beginning of the table up to the 3rd item, so {1, 2, 3} (as Python is 0-indexed)

Does this kind of syntax exist in Lua?

Thanks

If I understood the question properly this should help.

local t = {1,2,3,4,5,6}
local t1 = {} 
table.move(t,1,4,1,t1)
for index,value in pairs(t1) do print(value) end  --> 1,2,3,4
for index,value in pairs(t) do print(value) end -- 1,2,3,4,5,6

method : table.move()

Moves elements from table a1 to table a2 , performing the equivalent to the following multiple assignment: a2[t], ... = a1[f], ..., a1[e] . The default for a2 is a1 . The destination range can overlap with the source range. The number of elements to be moved must fit in a Lua integer.
Returns the destination table a2 .

In the link below you can check this method by roblox and what arguments should be passed into the 5 parameters.
Source : Tables - Devwiki

1 Like

Ah this definitely helps, thank you so much! :slight_smile:

1 Like