Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
1

What is the difference between index and *underscore*?

Asked by 1 year ago
Edited 1 year ago
i , v in ipairs | _,v in ipairs

What is the difference between those two?

0
I think you use _, when you don't need the index. --_(*-*)_-- ady1111 8 — 1y
0
There is no difference. '_' and 'i' are just variable names. DeceptiveCaster 3206 — 1y

1 answer

Log in to vote
0
Answered by 1 year ago
Edited 1 year ago

When you have a table and want to cycle through it, you have two options. ipairs(a) and pairs(a). I do not know the detailed implementation of each, but what they do is that they move through a (in this case a list) and assign values to variables. Let's focus on ipairs(a) here. What ipairs(a) does it returns the index and value of each entry in the key. For example, a simple loop like this:

local a = {1, 3, 4, 2}

for i, j in ipairs(a) do
    print(i, j)
end

...will print:

1 1
2 3
3 4
4 2

The thing is, many times you do not need the index, and _ is a "throwaway" variable, meaning you do not need the value assigned to it.

I don't exactly know how to explain this properly, but I think you can find a better answer here.

0
Wait can I just say "i , _" and it replaces the v with _? RebornedSnoop 170 — 1y
0
@RebornedSnoop, yes you could, as they are variable names 123nabilben123 472 — 1y
Ad

Answer this question