Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

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

Asked by
Vividex 162
10 years ago

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

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

Locked by adark and Articulating

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?

2 answers

Log in to vote
3
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

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
Ad
Log in to vote
-4
Answered by 10 years ago

:/ 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.

0
Do you happen to know what _G. is? Vividex 162 — 10y
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. BlueTaslem 18071 — 10y
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) BlueTaslem 18071 — 10y