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

What is the difference of "i, v" and "_, v" in for loops?

Asked by
DesertusX 435 Moderation Voter
3 years ago

Time to time, I see in for loops something like for _,v in pairs (table) do and I have no idea what it means. I understand that these are just defining a variable but what does the underscore mean? How is it different from i,v? If you can, can you also please tell me why most people use for i = in for loops? Help is greatly greatly appreciated. Thank you.

0
Aparently they are the same? LUCATIVIOMAD2 54 — 3y
0
So what does the _ represent? DesertusX 435 — 3y
0
i think they are the same and the _ represents the i but im not sure LUCATIVIOMAD2 54 — 3y
0
i rarely use it LUCATIVIOMAD2 54 — 3y
1
Look at it as a variable. You can name it whatever. So i and _ are just names Spjureeedd 385 — 3y

3 answers

Log in to vote
3
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
3 years ago
Edited 3 years ago

The character itself doesn’t have much significance, it just simply represents an ignorance figure—a blank space. Even though it still is supplied with the identical index data, we still use it a form to conventionally signify that the parameter is insignificant to actions we’re performing inside the iteration function.

Here is a example to help put the information above into perspective:

local arbitraryTable = {1,5,true,"Hello!", nil}
for index, value in pairs(arbitraryTable) do
    print(index, value)
end

--[[ Output:
1    1
2    5
3    true
4    Hello
5    nil
]]

In that code, we wanted to use the current index (where were are in the table) and it’s affiliated element. But let’s say we didn’t want to use that index, we signify that like so:

local arbitraryTable = {1,5,true,"Hello!", nil}
for _, value in pairs(arbitraryTable) do
    print(value) --[[ We mark that we don’t want i, 
    and as you see it isn’t used at all.]]
    --[[ print(_) If you run this, you’ll see _ still 
    contains the information anyway]]
end

--[[ Output:
1
5
true
Hello
nil
]]

This can be done vice-versa, as it really doesn’t matter how you use _. Like I said, it’s not significant to the language at all, so technically wherever we don’t want to use a space, we fill it with “nothing” by using _. You can write for i,_ in pairs(table) do for all that matters.


As for for i =, this is identical to the behaviour as i,v, it simply provides us with a variable local to the function scope that contains information that is determined by the function itself. In this case, i will represent a # based on the iteration principles given: start, stop, skip:

for i = 1,5 do --// *start* at 1, go to 5 *stop at*
    print(i)
end

--[[ Output:
1
2
3
4
5
]]

for i = 0,10,2 do --// Skip by 2
    print(i)
end

--[[ Output:
0
2
4
6
8
10
]]
0
Thank you! The exact answer I was looking for! DesertusX 435 — 3y
0
But what does "i" represent? Like, why specifically "i" rather than any other letter in the alphabet? (I mean for the last example) DesertusX 435 — 3y
0
It can be assigned to anything we wish, but for conventionally, it means Index. Ziffixture 6913 — 3y
Ad
Log in to vote
2
Answered by 3 years ago

They are the same. However, you cannot read the value of the index if you set the name to _.

0
_ is the same as any other variable name ScuffedAI 435 — 3y
1
the reason for why people give it that vairable name is because it's not meant to be used. ScuffedAI 435 — 3y
Log in to vote
-1
Answered by
Borrahh 265 Moderation Voter
3 years ago

No Difference, the first variable is the Index and the second one is the Value. All the following ones work:

For i, v in pairs
For ind, var in pairs
For i, player in pairs

Etc

0
Thanks! DesertusX 435 — 3y
0
Since I helped, can mark my Answer pls Borrahh 265 — 3y
0
That's not really what I meant though. I already knew this. What I was asking was what does the _ represent. DesertusX 435 — 3y

Answer this question