Items a to b of table in lua

For extremely large sequences you will need write your own function to do this. But for most uses Lua’s built in table.unpack will do the trick:

Lua 5.4.1  Copyright (C) 1994-2020, PUC-Rio
> t = { "a", "b", "c", "d", "e", "f" }
> table.unpack(t, 2, 4)
b       c       d

table.unpack simply returns the elements of the sequence, so if you want a sequence you will need to use a table constructor:

> { table.unpack(t, 2, 4) }
table: 0x229d180

You can bind the table to a variable, or iterate over it directly:

> for k, v in ipairs{ table.unpack(t, 2, 4) } do
>> print(k, v)
>> end
1       b
2       c
3       d

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top