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

What is the different between for _,v and for i,v ?

Asked by 6 years ago

I'm very confused on what the difference is between for _,v and for i,v. Here is an example of those 2 for statements being used;

for _,v

local Names = {"Player1","Player2","Guest1000"}

for _,names in pairs(Names) do
    --blah blah blah codes idk
end

for i,v

local Names = {"Player1","Player2","Guest1000"}

for i,names in pairs(Names) do
    --blah blah code idk xd
end

Any help?

2 answers

Log in to vote
1
Answered by 6 years ago

What's the difference between for _,v and for i,v? Actually, there's no major difference. When you use a _ (underscore) you're actually just removing the index (or i as most people define it). The variable(s) within a for loop can be defined as anything.

Examples For example, I could do:

local myTestTable = {"Hello World!"};
for index, value in pairs(myTestTable) do
    print(index, value);
end

That's equivalent to doing:

local myTestTable = {"Hello World!"};
for i,v in pairs(myTestTable) do
    print(index, value);
end

Conclusion Basically name the variables in your loops the way you'd like to name them. Back to ,v if you know you're not going to use the index (i) then some developers will just put an underscore (). When they put that underscore they know they won't be using the index and it's used to prevent the defining of another variable.

I hope I answered any questions or concerns you may have had :)

0
You are not removing the index. It is just renaming it, so you need to do _ to access it. _ is the placeholder variable, so people use that when they don't need that part. hiimgoodpack 2009 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

Some people like using for _,v instead of for i,v because they like eliminating the index value as a reminder that they're not going to use it in their code. It's just a style to.

I might be wrong, I don't know either o-o

Answer this question