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

What does for _, in pairs mean?

Asked by 9 years ago

I've always seen this when scripting, or looking at others script. I mean, I know what for i, v in pairs do does. But every time I see this, I wonder, "What the does mean?" And I know, I have to include snippets, but, of MY script. Which I can not make without understanding the significance of this. for _, part in next,game.Selection:Get() do part.CFrame=part.CFrameCFrame.new(0,0,0)CFrame.Angles(math.rad(0),math.rad(0),math.rad(0)) end I saw this in the Wiki when searching for CFrame. And I need this answered, I recently started scripting, and I have a list full of things I need to learn. Not only that, but, I've also seen this for u, c in pairs do. Please answer this.

0
Could you please edit your post to use Lua code formatting on the Lua? Also: The "_": the underscore: https://scriptinghelpers.org/questions/7815/what-does-a-blank-mean It isn't actually ignored by Lua, it just indicates that the author of that code wanted to ignore it (usually) BlueTaslem 18071 — 9y
0
I wanted to actually do that. But when I attempted to, it made no sense. I want to get answers, not confuse you guys :/. @BlueTaslem offbeatpizzalives123 105 — 9y

2 answers

Log in to vote
2
Answered by
Lacryma 548 Moderation Voter
9 years ago

Pairs, next, and ipairs are all used to iterate through tables.

Pairs:

local tab = {"I", "like", "pie"}

for i,v in pairs(tab) do
print(i, "\t", v) --\t is an escape for tab.
end

Output:

1 I 2 like 3 pie

Next:

local tab = {"I", "like", "pie"}

for i,v in next, tab do
print(i, "\t", v)
end

Output:

1 I 2 like 3 pie

Another usage for next:

local tab = {"I", "like", "pie"}

print(next(tab, 2))

The usage here is next(table, index)

Output:

3 pie

Ipairs:

local tab = {"I", "like", "pie"}

for i,v in ipairs(tab) do
print(i, "\t", v) --\t is an escape for tab.
end

Output:

1 I 2 like 3 pie

The difference you say?

While pairs goes in no particular order, ipairs goes directly in the order the table is organized in and next goes directly to the next iterator.

Now for the example you have provided:

for _, part in next,game.Selection:Get() do

This is mostly used in building tools, when a few parts are being selected, the script can get all the parts selected into a table and do an action with them.

Example:

for _, part in next,game.Selection:Get() do
part.CFrame = part.CFrame + Vector3.new(0, 5, 0)
end

This makes the whole selection of parts go up 5 studs from where they were beforehand.

Further Reading:

Pairs

iPairs

0
Either I am using blockcode wrong or it is broken. Lacryma 548 — 9y
0
The backticks are only for inline code. If it's multiple lines you have to use the Lua thing, or just specify code separate for each line. Or you could use blockquote (> ) BlueTaslem 18071 — 9y
0
Thank you. Lacryma 548 — 9y
Ad
Log in to vote
0
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
9 years ago

The underscore ( ' _ ' ) is a valid variable name in Lua, and is typically used to represent return values you're not intending to use. In the example of the pairs iterator function, it returns the index and the value of the Table it is looping through. If you don't need the index, you use a '_' to represent that. If you don't need the value, you just ignore it and only supply a variable for the index.

As for the in next, Table do bit, that is simply pulling the contents of the pairs function into the loop directly. It is functionally identical to in pairs(Table) do.

0
I also don't get what it is, but what I think I am reading is that it just ignores the "i" in "for i,v in pairs"? @adark Vividex 162 — 9y
1
The "_": the underscore: https://scriptinghelpers.org/questions/7815/what-does-a-blank-mean It isn't actually ignored by Lua, it just indicates that the author of that code wanted to ignore it (usually) BlueTaslem 18071 — 9y

Answer this question