What are all of the for pairs and how do they work?

Achieve & Issue

I am attempting to understand all of the for pairs in lua.
No matter where I look, I never can seem to find anything on the pairs and how they work.

Solutions

I’ve looked on the Lua Site, other scripting sites and the ROBLOX Developer Page and I can’t seem to find where these pairs are.
The only pairs I know of is for i,v in pairs () do. I also barely know how that pair works.

Anything that starts with the for i idea is what i’m talking about.

Thank you for any help you can give me.

pairs is just iterating function, just like ipairs and next are.

1 Like

What I mean by my question is I don’t know what any of the pairs are at all. Meaning, I don’t know what a ipair is or how to use them in a script properly.

next() works about the same as pairs(). ipairs() goes in numeric order, which stops at nil values. However, does not work well with non-list.

What all these functions have in common is picking out an index out of a table and its key.

1 Like

Except next is slightly faster but difference isn’t really noticable.

Performance is irrelevant between both as it is just micro-optimizations, difference is rather small. pairs() uses next(), for your information.

Numeric loops are determined to be the fastest out of all. However, all have different usage in situation. It is depending on context whenever to use them.

1 Like

Yeah as I said, pairs() is iterating function that’s used in for loops. Here’s an example of a generic for loop for i, v in pairs(table) do. Now how this loop works is, it’ll iterate through given table, but unlike numeric for loop it won’t iterate in numeric order meaning it won’t go from the first object in a table to the last object in a table, but it’s gonna be a random order. Now you may be wondering what i and v are for - i stands for index of the current object while v is it’s value/key.

Only pairs and ipairs. This is the kind of thing you can search up because there are enough resources on the DevForum, Developer Hub and internet (including Lua’s website) that explain all of this.

https://www.lua.org/pil/7.3.html

2 Likes
posted too early dang it

The format of a generic for loop (not “for pairs”) generally goes like this

for returns in iter, invariant, init do

end

returns is just the variables that get the returns of the function iter. The invariant state is invariant, init is where to start. the loop keeps going until iter returns nil.

I will use a reverse ipairs function example

local function iter(t, i)
    i = i - 1
    local v = t[i]

    if v ~= nil then
        return i, v
    end

    return nil
end

for i, v in iter, {"a", nil, "c"}, 4 do
    print(i, v)
end

I hardcoded the length of the table + 1, so how it works is on the first iteration it goes iter(t, #t + 1), second one it goes iter(t, #t), third iter(t, #t - 1), and so on until t[i] is nil, whether it be because the end of the table was reached (well, beginning since this goes in reverse lol), or because of a hole in the middle.

Now functions like ipairs and pairs are known as iterator factories or iterator generators because realistically, all they do is return the functions that do the work, and arguments for it.

pairs is as simple as

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

Now to wrap the iterator just wrote, the one that goes backwards,

local function reverse(t)
    return iter, t, #t + 1
end

And use it the same as pairs or ipairs;

for i, v in reverse({"a", nil, "c"}) do
   print(i, v)
end

Same exact result, but the wrapper just make it easier to read.

As for next being faster than pairs, that is literally so small difference you can’t even consider it faster. And Luau optimised pairs and ipairs but not next i don’t think.

9 Likes

This is not true in Luau. Using pairs in a loop has custom optimization behavior.

4 Likes

Yeah yeah I know, but as I said, the difference is so small as if there was no difference

1 Like