May someone explain to me what for _, in pairs means?

I am currently, learning to script, and I think I’ve gotten most of the basics down, but I am still confused on what for_i, pairs does, and what it means. I’ve tried to look for explanations, but I haven’t any that make sense, or are related to my question. I can understand for i, v in pairs, and for i = 1,100,0.1(for example), but I can’t quite grasp this concept.

ah ok, and sorry, Im still new to the platform

_ is a variable actually, it is used mostly to indicate that variable isnt going to be used, other then that its just a normal variable, people just made that convention

The use of an underscore as an identifier doesn’t really mean anything special in Lua, or in most languages for that matter. Programmers use it to represent nothing. Say, for instance, you had a function that returns 2 values, but in your situation the first one doesn’t matter. You can’t just skip it, you still need to account for it because the order of how data is returned is based on position. For instance:

local function get_name()
    return "John", "Doe"
end

local _, last_name = get_name()

Because defining first_name would really be unnecessary, an underscore is used and that is how programmers say they won’t be using that variable.

However an underscore is still a valid identifier so you can still use it.

print(_) -- John

But that kind of ruins the whole purpose.

This same logic goes for the for _, v in pairs(t) do idiom. Generally you don’t need the index but only the value.

6 Likes

ok, thank you I am kind of understanding it now