pairs() is a function that iterates through a table and returns three values for each item in the table: the
next function, the
table and
nil.
That means you can traverse through all elements in a table like so:
for key, value in pairs(table) do
-- do something with key and/or value
end
the variables
key and
value here can be any arbitrary variable name you like. The following would be just as valid:
for banana, apple in pairs(fruits) do
-- do something with banana and/or apple
end
The first variable always represents the key and the second value always represents the value for that key.
Example: Let's say you have the following table:
local player = { firstname = "Peter", lastname = "Parker", age = 25 }
The keys would be
firstname,
lastname and
age, whereas the value would be "
Peter", "
Parker" and
25.
If you'd do this …
for key, value in pairs(player) do
print(key.." -> "..value)
end
… it would generate an output like this:
age -> 25
firstname -> Peter
lastname -> Parker
If you had a table without custom keys like this…
local fruits = { "Melon", "Orange", "Apple", "Kiwi", "Banana" }
…then the keys would be the index at which the values exist in the table and the output would look something like this:
1 -> Melon
2 -> Orange
3 -> Apple
4 -> Kiwi
5 -> Banana
Then there's
ipairs() which only returns indexed values, i.e. only values that don't have a custom key.
Consider the following code:
local mixed_table = { "Marmelade", tool = "Hammer", animal = "Zebra", 23, "Bluebird", flower = "Tulip" }
for key, value in pairs(mixed_table) do
print(key.." -> "..value)
end
This would produce an output like:
1 -> Marmelade
2 -> 23
3 -> Bluebird
flower -> Tulip
animal -> Zebra
tool -> Hammer
As you can see it listed the indexed entries first.
Now if you use
ipairs() instead of
pairs, the output would look like this:
1 -> Marmelade
2 -> 23
3 -> Bluebird
It only listed the indexed entries and skipped the ones with custom keys entirely.
So what is the
_ character?
It's simply an arbitrary variable. In Lua an underscore is a perfectly valid variable name.
Many programmers just use
_ as a crutch so that someone reading the code knows that this variable is not important and won't be used. It just acts as a placeholder.
For example, if you would only want the values of a table, you couldn't do something like this:
for value in pairs(mixed_table) do
print(value)
end
pairs() always returns a
key first and a
value second. So if you only assign one variable to the output of
pairs(), then whatever
pairs() returns first will be stored in that variable.
That means that in the code above,
value would actually always contain the current
key.
If we want only the
value, we have to assign our variable in second place. Ergo we have to assign some random variable in first place.
We won't need it, so it can be named anything we like. And a good way to see immediately that we only use that as a placeholder, is to use something like e.g. an underscore:
for _, value in pairs(mixed_table) do
print(value)
end
And vice versa, i.e. if we only care about the
keys and not the values, we could do this:
for key, _ in pairs(mixed_table) do
print(value)
end
But again, it doesn't matter that we chose an underscore. You can just as well choose e.g. FUBAR:
for FUBAR, value in pairs(mixed_table) do
print(value)
end
Since it's a perfectly valid variable name you could still make use of _ (or FUBAR) and access it whenever you like.
It's just a crutch for easier recognition by other programmers, or even yourself.
There's still a bit more detail, but this should be the gist of it.
More info:
Roblox - Core Functions: pairsLua PIL - pairsLua PIL - 7.1 Iterators and Closures