Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 9 years ago

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

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

1 answer

Log in to vote
2
Answered by
ImageLabel 1541 Moderation Voter
9 years ago

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
Ad

Answer this question