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

What is "_(letter)," and whats the difference between pair and pairs?

Please include an example of when you would use it, thanks!

And by "_(letter)," i mean like "_v,"

2 answers

✓ Accepted answer

1) _ is treated by Lua as a letter: exactly the same as A or e or F. For stylistic purposes, it's sometimes used as a word or namespace separator: Ex.: apple_pie sourceEngine_lightFires

When it precedes a name, it usually indicates that the name is somehow special or not intended to be accessed. For instance, a helper function for a factorial may be called _factorial, and the function you are supposed to call is called factorial. Other examples are the metamethods, which are all preceded by two underscores: Ex.: __index

Finally, sometimes we use a single underscore to indicate that a variable is not important. Because of how multiple returns work in Lua, this is sometimes necessary.

For example, string.find locates the first and last characters of a found string. So if we only cared about the last character, we might see someone doing something like this:

_,last = str:find("cat");
-- last is position of "t" in "cat" in `str`
-- we don't care about _ so we called it that to indicate that

All of this said, those are just common stylistic considerations. There is nothing special about the underscore.

All of the following are valid, normal names that act like any other function or variable name:

_under_SCORE
_sdofij_WOEIJFOIJ___SDOIFJOSDIJ
_sdoifj_____
______
_2_3_4_
___
_
_______

2) pairs and ipairs both iterate over tables. pairs iterates over all keys/properties/indices, while ipairs only iterates over those which are counted in table length (#), which are the values 1, 2, 3,... until a value is found which is nil.

This means that only "a","b","c" will be printed out for the following, even though the positive integer key 5 is defined for the table:

local t = {"a","b","c","d","e"};
t[4] = nil;
for i,v in ipairs(t) do
	print("t[",i,"] = ",v);
end

:/ You can do anything to index with pairs. For example, I could do this.

for i,v in pairs(workspace:children())
v:Destroy()
end

Or this

for potato, salad in pairs(workspace:children())
salad:Destroy()
end

There is no "pair". It is only pairs or ipairs.

3 comments

0

Do you happen to know what _G. is?

0

_G is one of the built in variables to Lua (just like math, table, or print (or like, to ROBLOX, wait or game)). Specifically, it is just a table ({}); usefully, it is the same table in every script. So, _G.apple = 5; will change the value of _G.apple in all scripts, not just the one you are editing.

0

You seem to be under the impression that _ is special. It is treated exactly the same as any letter by Lua. Because it looks different, people frequently use _ when it's a variable that they don't care the value of (for example, instead of using i,v they use _,v because the index is not being used by that snippet of code, because it does not mean anything in that case)

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