Pairs is a function that runs next internally.
It returns three values - the key, the value, and the iterator function. You can read more on this on the manual available on www.lua.org
ipairs loops through all sequential numeric keys in a table starting at 1.
So if I have this table:
Code:
food = {
"apple",
"banana",
"cantalope",
"fruit that starts with a d",
Price = 12.54,
nil,
"eggplant"
}
And used this loop:
Code:
for k, v in ipairs( food ) do
print( k, v )
end
I get this:
Code:
1 apple
2 banana
3 cantalope
4 fruit that starts with a d
Because it sees the first four keys, and key five is nil so it stops. It never goes through non-numeric keys, such as Price in the above example.
Now, if I just use pairs:
Code:
for k, v in pairs( food ) do
print( k, v )
end
I get:
Code:
1 apple
2 banana
3 cantalope
4 fruit that starts with a d
6 eggplant
Price 12.54
Because it finds every valid key in the table. Take note that pairs doesn't care about the order of the table and will not always return items in the same order.
The usage of _ in the key/value setup is a habit some people use to mark that they don't care about that value. It's legal and has no real effect on the code.
Feel free to ask me to clarify parts of this if any of it is unclear.