PICO-8 Wiki
Advertisement
all( tbl )
Returns an iterator for all non-nil items in a sequence in a table, for use with for...in.
tbl
The table to iterate.

return-value
An interator function that can be used with for...in to iterate over tbl.

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

Specifically, all() starts at index 1, then steps forward through the table until it reaches the final index, emitting only the elements which are not nil.

Take care when manipulating the table itself while iterating through it. The all() iterator does not track the index, so the only way to delete elements during iteration is to use del(table, value). This will work fine if there are no duplicate values in the table, but if two elements of the table contain the same value, it's possible that you might delete one before the current iteration point and confuse the iterator. The all() iterator is capable of recovering after the element at the iteration point is deleted, but not elements before it. If the table might contain duplicates, it is advised to use the ipairs(table) iterator, which supplies both index and value, and deli(table, index) to delete the element at the current index.

Examples[]

<syntaxhighlight lang="lua"> -- note this is only guaranteed to work correctly with -- a table that has no duplicate values, except nil gaps t = {1, 3, nil, 5} add(t, 7) add(t, 9)

for v in all(t) do

 print(v)  -- prints 1, 3, 5, 7, 9

end

function no_primes(t)

 for e in all(t) do
   if is_prime(e) then
     del(t, e)
   end
 end

end </syntaxhighlight>

See also[]

Advertisement