Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference of pairs() vs. ipairs() in Lua?

Tags:

for-loop

lua

People also ask

What is the difference between pairs and Ipairs Roblox?

ipairs is best but it needs to be an array without gaps. pairs must be used for dictionaries or arrays with gaps. The only “error” you could have with pairs is that the order is not guaranteed. With ipairs it goes through the array in order, hence needing to be an array with no gaps.

What does Ipairs mean in Lua?

The ipairs() function will allow iteration over index-value pairs. These are key-value pairs where the keys are indices into an array. The order in which elements are returned is guaranteed to be in the numeric order of the indices, and non-integer keys are simply skipped.

Are Roblox pairs faster than Ipairs?

As for OP's question, ipairs is equivalent to a numerical loop, making it faster when iterating over large ordered arrays. pairs is used to iterate over tables that aren't arrays or are unordered.

How do you break a loop in Lua?

Lua, like most lanuages of this kind, has a "break" command that jumps out of the smallest enclosing loop. Normally you use "break" with "if" to decide when to exit the loop.


pairs() and ipairs() are slightly different.

  • pairs() returns key-value pairs and is mostly used for associative tables. key order is unspecified.
  • ipairs() returns index-value pairs and is mostly used for numeric tables. Non numeric keys in an array are ignored, while the index order is deterministic (in numeric order).

This is illustrated by the following code fragment.

> u={}
> u[1]="a"
> u[3]="b"
> u[2]="c"
> u[4]="d"
> u["hello"]="world"
> for key,value in ipairs(u) do print(key,value) end
1   a
2   c
3   b
4   d
> for key,value in pairs(u) do print(key,value) end
1   a
hello   world
3   b
2   c
4   d
> 

When you create an tables without keys (as in your question), it behaves as a numeric array and behaviour or pairs and ipairs is identical.

a = {"one", "two", "three"}

is equivalent to a[1]="one" a[2]="two" a[3]="three" and pairs() and ipairs() will be identical (except for the ordering that is not guaranteed in pairs()).


There is no array-type in Lua, only tables which might have consecutive elements starting from index 1.

The generic for-loop, in contrast to the numeric for-loop, expects three values:

  1. A callable
  2. A context-value it passes on
  3. An initial index-value

It calls the callable with context-value and index-value, storing all the returned values in the provided new variables. The first one is additionally saved as the new index-value.

Now some representative examples of callables for the loop:

  1. ipairs(t) returns a function, the table t, and the starting-point 0.
    The function is the moral equivalent to:

    function ipairs_next(t, i)
        i = i + 1
        var v = t[i]
        if v ~= nil then
            return i, v
        end
    end
    

    Thus, all numeric entries starting at 1 until the first missing one are shown.

  2. pairs(t) either delegates to t's metatable, specifically to __pairs(t), or returns the function next, the table t, and the starting-point nil. next accepts a table and an index, and returns the next index and the associated value, if it exists.

    Thus, all elements are shown in some arbitrary order.

  3. There are no limits to how creative one can be with the function, and that is what vanilla Lua expects.
    See "Bizzare "attempt to call a table value" in Lua" for an example of a user-written callable, and how some dialects react if the first value is not actually a callable.