Simple scripting question (table question)

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.

1 Like

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”.

1 Like

Ok, thank you for the reply. So when im looking at the code in this, I can base the table and the variable names used for it on the order they are in?

This is a for index, value loop index would be the position of a thing in a table (Also can be the key of a value if it is a dictionary)

Tables have an Index and a Value Which are both Customizable.
If you do
Print(i,v) your table your output would look like this

1 apple
2 apple
3 apple
4 orange
5 orange
6 orange

But you can customize so it could become

local table  = {["Item 1"] = "Apple", ["Item 2" = "Orange"]}
--Output print(i,v)
Item 1 Apple
Item 2 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
1 Like

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)

1 Like

This is now a dictionary, no longer a table.

1 Like

Dictionary is still a table though.

1 Like