Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

I don't understand Arrays, can you help me?

Hello, i don't really understand "Arrays". Can you please explain it to me?

1 comment

0

Lua doesn't have a separate concept of "array". It mixes arrays and hashtables/hashmaps/dictionaries into a structure it calls a "table"

1 answer

✓ Accepted answer

Array

A table with sequential numerical indexes (integers), is more or less an array. As the values, which can be anything from simple integers to strings and objects are increased, the indexes also increase accordingly.

Arrays in Lua

In ROBLOX, and Lua in general, arrays are not a type, but there are ways to closely approximate it, using . Lua and also assumes 1-based array and string indexing, which explains why indexes start at one instead of zero. In more languages than not, 0-based array and string indexing are used.


Example of an array in Lua

 array = {1, 2, 3, 3.01, 'Scripting Helpers'}

Indexing

 array = {'Scripting', 2, 3, 3.01, 'Helpers'}
print(array[1]) --would return the value associated to index "1", `Scripting`
print(array[5]) --would return the value associated to index "5", `Helpers`

NB: In other languages, array[0] would return the first index's value, and multidimensional arrays are referred to as matrices.


Arrays can also be multidimensional like so:

myArray = {'Bonjour', {'Morning', {'Scripting', 'Helpers'},'Sunday'}, 'Au revoir'}

and would be indexed like so:

myArray = {'Bonjour', {'Morning', {'Scripting', 'Helpers'},'Sunday'}, 'Au revoir'}

myArray[1])
--> Bonjour
myArray[2][1] 
--Morning
print(myArray[2][2][1], myArray[2][2][2])
--> Scripting Helpers

This discussion is preserved as it was. New answers, comments, and votes are closed.