Whats the difference between _, v and i, v?

So, i was making my game, and i needed to use a , v loop in it, i used i, v but that made me start questioning myself

whats the difference between
_, v and i, v?

1 Like

There’s a very small difference. Usually, i means that the index for the value will be used in the provided code, while _ means that the index won’t be used. _ is commonly used to indicate a value won’t be used

They’re the exact same, they just have different names, just like variables:

local x = 3
local y = 8
-- is the same as
local _ = 3
local y = 8

Oh yeah i understand, but whats exactly index? i never really understood what it is

There isn’t a difference, and it’s all preferential… The equivalent for how this works is as follows…

local testVar = 0 
local _ = 0 

as you can see they both hold the same variable, but are identified differently.

The index is the identifier that’s used to retrieve a value from a table/array/dictionary:

local t = {
    ['Item1'] = 'Value1' -- "Item1" is the index
}

Ohh but like @HugeCoolboy2007 said, its so index wont be used right

Index is the thing that specifies the item in the table. For example, in:

local t = {
"First entry";
"Second entry";
"Third entry"
}

In this example, the entry “First entry” will be in slot [1], so doing

print(t[1])

would print “First entry”

Ohh i underdtand it now

character69420

It doesn’t mean it can’t be used, it’s just often used if it won’t be necessary to use. Using _ would still work.

Yeah i also didnt find a real difference between it now

So the index is pretty much also something that can have its named changed instead of being a _ right?

_, v and i, v are the same in terms of functionality. They’re both variable names. The only difference is that one variable is called _ but the other is called i. Nothing special happens internally it’s just a practice coders use to define a value that isn’t used. You can still use both variables.

Index is basically the object that calls the specifec value in a table. For example:

2 Likes

It’s essentially a placeholder variable (a way to store indices & values from arrays/fields & values from dictionaries while traversing over the items of a table), they can be set to whatever name (indentifier) you desire, as the first reply to this thread stated i is commonly used when an index/field is being used inside the loop whereas _ is frequently used to indicate that while the index/field exists it is not going to be used. You also don’t need to declare both, for example.

local tables = {"a", "b", "c", "d"}

for i in ipairs(tables) do
	print(i)
end

This will output 1, 2, 3 and 4 (the indices of the items in the array). All of the values end up being discarded.

I just wanted to point this out since many beginners don’t know this and nobody here explained it yet.

local t = {
    "a",
    "b",
    "c",
    "d"
}
local t = {
    [1] = "a",
    [2] = "b",
    [3] = "c",
    [4] = "d"
}

These tables are the same, the first one is just a shorter way of writing the second one.
That’s way the indexes are 1, 2, 3, 4 at a table like {"a", "b", "c", "d"}.

{[index] = value}

Thanks for all the help! you guys also helped me find out that index was exactly what i needed for a game im making!

They’re the exact same thing in for loops.


end

is the exact same as

for _,x in pairs() do

or

for g, d in pairs() do