Data Structures
Lua Arrays
Using Arrays in Tables
Lua arrays use tables with numeric indices starting at 1.
What is a Lua Array?
In Lua, arrays are implemented using tables, with numeric indices that start at 1. Unlike many programming languages where array indices start at 0, Lua's convention is 1-based indexing. This means the first element of a Lua array is accessed using index 1.
Creating a Simple Array
To create an array in Lua, you define a table and initialize it with values. Here's how you can do it:
Accessing Array Elements
Array elements in Lua can be accessed using their numeric indices. As mentioned earlier, these indices start at 1. Below is how you access elements:
Modifying Array Elements
You can modify elements in a Lua array by assigning new values to existing indices. Here's an example:
Iterating Over Arrays
Iterating over arrays in Lua can be done using the for loop. Below is an example of how you can iterate over an array:
Common Operations on Arrays
Lua provides several built-in functions to perform common operations on arrays, such as finding the length, inserting elements, and removing elements. Here are a few examples:
Data Structures
- Tables
- Arrays
- Dictionaries
- Metatables
- Strings
- Previous
- Tables
- Next
- Dictionaries