Sorry I am a noob trying to learn how to script. I wanted to ask a question about this example im looking at. the example is:
local basket = {"apple", "apple", "apple", "orange", "orange", "orange"}
for num, item in pairs(basket)do
if item == "apple" then
print("Cut apple")
elseif item == "orange" then
print("Peel Orange")
end
end
In this block of code, I dont understand what the num and item are representing after the “for”. Any help/clarification would be greatly appreciated.
The num is the index, while item is the value at that index in the table.
The for loop will run through the entire table, and the num will start from 1 to the length of the table.
For example, it will start at index 1 of the basket table and then store “apple” as item. And then it will go to index 2 of the basket table and then it will go to store “apple” as item and so on, until it reaches the last index of the table which is 6 which has the value of “orange”.
In pairs loop simply iterates through a table , It starts from the first value of the table to the last one.
A example of how people use it would be: for i,v in pairs(table) do
i would be index, if the table is a array then i will be the position of the value in the table. Let’s say there are 3 values inside it.
local myArray = {"apple", "pea", "banana"}
for i,v in pairs(myArray) do
print(i,v)
--Output will be 1 apple 2 pea 3 banana
end
Dictionary would be this:
local myDictionary = {
enabled = false
myFavoriteNumber = 500
}
--Since its a dictionary then i will be the key for each value (In this case i will be enabled and myFavoriteNumber while the v will be their values, false and 500)
for i,v in pairs(myDictionary) do
print(i.." = "..v) --Output: enabled = false myFavoriteNumber = 500
print(i,v) --Output: enabled false myFavoriteNumber 500
end
That’s only obviously for numerical indexes, better to say ‘num’ would just represent the index (whatever it is).
It will just store the ‘item’ variable as whatever the value is on each iteration, not the other way around.
dictionaries and tables aren’t different, a table is a blanket term for both arrays (according to Roblox, only numeric indices otherwise generally associative arrays can have anything other than NaN and nil as an index) and dictionaries (non-numeric indexes) both, also dictionaries still need a separator which I think you forgot to add.
@Tsunen refer to this post to know (other than what previous posts here have already mentioned) what the num and item represent
you can store the index and value with any variable you want, people commonly use i, v in reference to the current index, value.
Again, a table and an array are still the same things.
Yes I know that dictionaries are tables, thats why when I went to explain about arrays I said “if the table is a array”, I forgot to specify if the table is a dictionary but yeah I think it is understandable
If this helps clarify, in for i, v in pairs(table)
The for loop is looping through the table (also called an array). “i” is the number of times the loop has run, starting at 1, while “v” is the item in the array you are currently at (table[i]).
Each loop, “v” will become the next item, and “i” will increase by one, until you have looped through the entire table.
To loop through the table in order, do for i, v in ipairs(table)