Indexing and slicing
numluau allows you to index arrays in many ways.
indexing¶
Indexing works similarly to how you'd index tables. Arrays are 1-indexed, meaning arrays start from 1.
We can use a negative index to index from the back of the array instead of the front
Arrays can also be indexed using strings, allowing you to index is other ways.
If we wanted to index a 2d array, we could use regular table syntax.
Or we can use the index col, row to also get that item.
slicing¶
Sometimes you dont want to get a single item, but instead a collection of items. This is what we call a slice.
1d arrays¶
Using the index first:last:step, we can retrieve a portion of the array with only some items.
local a = numluau.array({1,2,3,4,5})
-- get items 1-3
print(a["1:3"]) -- array([1 2 3])
-- get every second item
print(a["1:5:2"]) -- array([1 3 5])
When we create a slice, it doesn't create a whole new copy. This means if we mutate the slice, it will affect the original.
You can also leave a parameter empty and it will assume the value for you.
2d+ arrays¶
We can also apply this notion to higher dimensional arrays
local a = numluau.array({
{1,2,3,4},
{5,6,7,8}
})
-- gets the entire 2nd row
print(a["2"])
-- gets every item on the 2nd column
print(a[":,2"])
We can expand this to multiple dimensions by combining first:last:step and col, row.