Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

What is the difference between index and *underscore*?

i , v in ipairs | _,v in ipairs

What is the difference between those two?

2 comments

0

I think you use , when you don't need the index. --(-)_--

0

There is no difference. '_' and 'i' are just variable names.

1 answer

✓ Accepted answer

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.

2 comments

0

Wait can I just say "i , _" and it replaces the v with _?

0

@RebornedSnoop, yes you could, as they are variable names

This discussion is preserved as it was. New answers, comments, and votes are closed.