#What does index mean in Lua programming language
49 messages · Page 1 of 1 (latest)
think of it like this
local Index = {"1", "2", "3"}
print(Index[3])
i dont know how else
to give a good example
** You are now Level 6! **
does the letter "i" is an abbreviation of index?
from this
yes it does
okay thanks
nvm
The index is like the key to access a certain value inside of an array, for example, we have this array
local prices = {3, 9, 5, 20, 15}
This array has a size of 5 elements, where the starting index is equal to 1, that means if we want to access the 3$ price inside of it we need to do
prices[1] -- This is equal to 3$
If we wanted to get the next we would just add 1 to the index, and so on. You need to have in mind that index starting from 1 is actually pretty rare and just a few languages have this feature, most of them use 0 as their starting index, its all the same but instead of using 1 to get the first number, you use 0
Have you used Excel before?
Okay, so, in Excel, you have cells, like A1, A2, A3, etc
yes.
okay
simply
local Pets = {
Dog,
Cat,
Sheep,
Cow
}
print(Pets[1])
this is an index
it holds the names of
pets, whatever
and you can use it
for many
useful things
like create pet systems
, etc
In excel, if you wanted to get the value thats in the cell A1, you would just go and do in a different cell =A1, right? Well, that 1 after the A is the index, the position of that cell, since there are a lot of cells you need to identify them somehow and we do this by indexing them, like A1, A2, A3, A4 ... A99999999,
Here in lua its the same, the index is just the position where some value is stored in, lets do this:
In Excel we can do this table:
A1: Jhon
A2: Peter
A3: Hannah
And get for example, Jhon by using A1, because it is stored in the first position, 1
In an array its the exact same, lets do it in Lua
local names = {"Jhon", "Peter", "Hannah"}
As you can see, the position remains the same, Jhon is first place, Peter is second and Hannah Third
Again, if we wanted to get the name for Jhon we would do the same as in Excel
print(names[1]) -- Prints Jhon
The index is just the position, the address of something you have stored there, by using it you are asking the computer "Hey, can you give me whats on the first cell of the array Names?" And the computer will answer you with "Yes! In the cell number 1 of Names there's this Jhon string!"
👍
much easier to understand
👍
does index mean storage?
** You are now Level 2! **
its just what we call wherever place it is in an array