Can someone explain to me in pair loops more clearly?

Hi i am a scripter but something that I just skipped and didnt really understand are in pair loops can someone explain them to me more clearly please? Thank you anything is appreciated! :slight_smile:

1 Like

it basically just goes through a table and gets the stuff inside of it 1 by 1. Like this

local stuff = {"Block", "Table", "Food"}

for i,v in pairs(stuff)
print(v)
end

“v” would be the value and “i” would be the index

1 Like

Oh ok but why is their written i, v in pairs what does that mean?

the i,v are what you would call it, you could change it anytime to be E, F or other things too. Also, im not good at all the technical stuff, so it might be hard to understand

It can be any letter? So I can do u, i ? what it be the same result?

yea, it would be the same result. it’s basically just naming a variable.

oh ok i see thanks for the help :slight_smile:

Because I in a table the number ID is this [1] which represented by the I and v is the value. aka i = int v = value.

Well, its not always a number ID, thats just how it is for that type of table.

Well the “number ID” can be changed to strings or numbers etc. But the basic tables use numbers like 1,2,3,4 depending on the order they were added.

1 Like

you can change i and v to anything, they are variables so you can even do

for ok12345, abcde in pairs(Table) do

end

or

for abcde, ok12345 in ipairs(Table) do

end

Guys what do you mean by number ID

Number ID is just what I call this: in a table [1] in a table {[1] = “Ida”}

This description skims over a lot of information (although the core concept is correct, it does not actually explain much).

For starters, please use pairs with dictionaries, and ipairs with arrays. Do not use pairs for arrays like you have done here.

Pairs will enable you to iterate over each index value pair of a dictionary at your discretion.

This thread has also been made countless times before, always search for existing threads before making your own:

guys don’t call it number id

it’s better to call it index, it’s not always numbers

pairs is amazing, lemme try to explain.

you perhaps have seen i and v when declaring a pair.
Those are your variables. It kind of depends on what you feed pairs but the general idea is that pairs iterates over objects.

The following example

Shows you how to iterate over a list object. The i references to the index of the item. In normal programming languages we start counting at 0 but if i’m not mistaking Lua starts at 1. This means that

  • Block has a index of 1, so i = 1
  • Table has a index of 2, so i = 2
  • Food has a index of 3, so i = 3

If the index is 2 it means v = Table in this case.
If index would be 3 v would be Food

Pairs also works with table datatypes. This changes the i and v a little bit. Lets take the same example but with a table instead:

local stuff = {
    Block = true, 
    Table = "Chair", 
    Food = 5
}

for i,v in pairs(stuff)
    print(v)
end

Cool, so is i still the index? The answer is No. In this case stuff has key, value pair objects stored into it which means that i == the key of the object and v is the value of the object.

  • Block i = Block and v = true
  • Table i = Table and v = “Chair”
  • Food i = Food and v = 5

If the index is Block it means v = true in this case.
If index would be Food v would be 5

I hope this explains it a little bit better

3 Likes