Is there a difference between for _,v in pairs()
and for i,v in pairs()
?
Just something I wanted to finally figure out with some help.
I havent found anything on the wiki yet.
There is technically no difference, as _
, v
, and i
in both of the examples are just variables used to catch the return values of the pairs
call(s). _
is a valid variable name, after all.
However, there is a semantic difference: _
is meant as a placeholder. Specifically, it denotes a variable you never intend to use. In the case of for _, v in pairs(tab) do
it implies that we don't care about the indices of the table we're traversing, only the values.
This is relatively uncommon practice, as almost every function call you make should return the more relevant stuff first, but, as with pairs
and ipairs
, it does come up on occasion.