all( tbl )

Returns an iterator for all items in a sequence in a table, for use with

The table to iterate.

The all() function is used exclusively with to iterate over all elements in a sequence in a table. Each value in the sequence is emitted in sequence order.

Specifically, all() starts with the element at index 1, then increments the index and emits each value until it finds no element assigned (nil).

Examples Edit

t = {1, 3, 5}
add(t, 7)
add(t, 9)
for v in all(t) do
  print(v)  -- prints 1, 3, 5, 7, 9

Technical note Edit

Iterating with all() is good because it makes your code both concise and easy to read. When you need to iterate over objects in a table, in order, it's often the preferred method.

It should be noted, though, that this iterator needs to do some extra work to traverse the table in order. This causes it to be a little slower, per iteration, than using pairs() (which does not guarantee order) or a purely-numeric for loop.

Luckily, this is seldom an issue, as the code inside most loops significantly outweighs this overhead. However, in some tight, high-performance loops, it may have a noticeable impact on throughput. In those cases, it may be worth considering other options.

See also Edit