I am just wondering if these both do the same thing.
I always use:
for i,v in pairs do
I never use:
for i = 1, #Bleh do
Not exactly.
The pairs function iterates over the key/value pairs in a table. With this function the key/value mappings do not have to be ordered numerically for it to look through all the pairs.
The ipairs function is more similar to a numeric for loop, as it does require numeric ordering of key/value pairs. However, it stops as soon as it finds a nil value so it may not loop over all the mappings.
local table = {"Apple", "Banana", [4] = "Donut", ["Peach"] = "Pear"} for key, value in pairs(table) do print(key, value) end -- Output: -- 1 Apple -- 2 Banana -- 4 Donut -- Peach Pear for key, value in ipairs(table) do print(key, value) end -- Output: -- 1 Apple -- 2 Banana for i = 1, #table do print(i, table[i]) end -- Output: -- 1 Apple -- 2 Banana
First one catches the value's integer (position) and value.
myTable = {"Hello", 10, true, function() return "worked!" end} for i,v in pairs(myTable) do print(i, v, type(v)) if type(v) == "function" then print(v()) -- Runs the function, returns the string and uses it in a print-function. end end --[[ Output: 1 Hello string 2 10 number 3 true boolean 4 function: xxxxxxxx function worked! ]]
The fourth value prints a bunch of characters, not necessarily just x's. It's the function's ID.
Second one uses integers to determine how many loops the for-loop should do. It also uses an identifier to help find out how many loops it has done.
startNumber = 9 stopNumber = 23 for i = startNumber / 3, stopNumber / 3 do -- 3, 7.666... print(i * 2) end --[[ Output: 6 8 10 12 14 ]]
It started with 6 since 3 was the starting number and it printed 3 times 2 (print(i * 2)). It ended with 14 since 16 would mean i is equal to 8 which is above the ending number (7.666...) and could not be included in the loop.
I'm gonna talk about how these actually work. Lua.org is a good place to go that.
for i = 1, #tab do
is a numeric for loop. It's the same as
for i = 1, 10 do print(i) end
which is pretty straightforward.
for i, v in pairs(tab) do
, however, is a generic for loop. It uses an iterator function. According to the wiki, an iterator function is just "a function that returns the next set of values each time it is called."
Before we talk about pairs
, let's cover ipairs
. It's actually pretty simple how ipairs works.
function iter (t, i) i = i + 1 local v = t[i] if v then return i, v end end function ipairs (t) return iter, t, 0 end
iter
is the iterator function, t
is the array, and 0
is the initial value. Lua then calls iter(a, 0)
, then iter(a, 1)
, then iter(a, 2)
etc.
pairs()
works similarly.
function pairs (t) return next, t, nil end
next
is a primitive iterator function in Lua. next(table, key)
will return the next key and value in the table, although in no specified order. next(table, nil)
will return the first key and value in the table, which is why pairs
also returns nil
.
You can also use next directly. Since pairs(t)
returns next, t, nil
, we can just do this:
for i, v in next, t, nil do
Or, since nil is nil,
for i, v in next, t do
I think the other answer already explained the practical differences, so I'll leave it at that.