Differences between pairs and next?

Hello, I was wondering, what’s difference between ipairs pairs and next?
I just need to know what’s next
ipairs - ‘organize’ from 1,2,3,4,5 etc…
pairs - organize random 5,12,4,2 etc…
next ? How can I use next too? :thinking:

10 Likes

They’re the same. In vanilla Lua, pairs is written as

function pairs(t)
    return next, t, nil
end

So that in a generic for loop it looks like this

for k, v in next, t do

end

But in Roblox, pairs doesn’t seem to return next, but overall the behaviour is the same. You should always use pairs tho, it is idiomatically correct way to traverse dictionaries/mixed tables, and ipairs only goes through the array part, and ipairs is the idiomatically correct way to go through the array part of a table

48 Likes

Pairs uses next() for its own definition as an iterator.

https://www.lua.org/pil/7.3.html
You can read more about it here.

3 Likes

So, it’s the same? or does it changes something? like, how would i use it for :GetChildren() or what could I use it for?

for i,v in next, game.Players:GetPlayers() do
print(v.Name)
end

Would this work? It’s the same as ipairs?
EDIT: Tested and it works, but what’s difference? I don’t get it

1 Like

That would work, it’s the same as pairs. It’s exactly the same, since pairs internally returns next (in Vanilla Lua, but there’s no functionality change in Luau). There’s no reason to use next in this context, but it’s useful for checking if a table’s empty (next(table, index) will return the next element in a table).

12 Likes

This is because next(table, index) returns the next index and the value associated with that index.

local a = {11, 22, 33}
print(next(a, 1)) -- 2 22

next(a, 0) -- error

So arrays in Lua don’t start with index 0 as @starmaq thought.

1 Like

That’s because next is the iterator function that pairs returns. I don’t think anyone was expecting the iterator function to return another iterator function.

5 Likes

I never use next unless I’m specifically controlling table traversal such as with a while loop. Using it in a for loop makes it look silly and unreadable, it’s ugly. It also ends up looking inconsistent if I deploy ipairs for arrays. One of my current projects just uses next for both arrays and dictionaries, looks awful.

The next function isn’t just identical to pairs. While pairs does return next, or even if it doesn’t, it’s still idiomatic to use pairs when iterating through dictionaries. Next has better utility in circumstances where you’re directly controlling traversal. As one such example, the DoCleaning method in Quenty’s Maid class will use next in a while loop to clear out a table because that process can add more keys to the dictionary which a standard for loop would not catch after it’s already executed.

9 Likes

The conventional terminology I’ve seen used and use myself is that pairs and ipairs are iterator factories while their first return value is the actual iterator.

As previously mentioned, the iterator pairs returns is next, I assume @incapaxx’s confusion comes from pairs({}) == next yielding false, but that’s because the pairs function has its own closure of next separate from what is exposed in the base library (see lbaselib.c in the Lua source).

5 Likes

Next goes through a loop in order
heres an example:

local table2 = {
"Index1",
"Index2"
}

for I,v in next,table2 do
print(v)
end

and it would print

| > Index 1
| > Index 2

It does not go through a loop in order, it just happens to only over the array part but that’s not guaranteed by the Lua language, just an implementation detail. The Lua wiki specifically says you should use ipairs or a numerical for loop if order is important.

Here, test this:

local t = {
  [1] = 1, [2] = 2, [3] = 3, [4] = 4
}

for k, v in pairs(t) do print(k, v) end
1 Like

The difference between ipairs and pairs is that :
Ipairs is used to loop through arrays and normal table while pairs is used to loop through dictionaries.

That’s not exactly how they are, they shouldn’t be limited to dictionaries only. Pretty sure there are other topics covering it.