Fandom

Pico-8 Wiki

Pairs

162pages on
this wiki
Add New Page
Comments0 Share
pairs( tbl )

Returns an iterator of key-value pairs for all elements in a table, for use with for...in.

tbl
The table.

The pairs() function is used exclusively with for...in to iterate over all elements in a table. It emits the key and value together, which you can assign to variables in the for loop:

for k, v in pairs(tbl) do
  -- ...
end

pairs() returns every value in the table, not just those with sequential indexes. It can return keys and values for tables being used as mappings (dictionaries) or objects.

pairs() does not guarantee the order in which the results are returned.

Examples Edit

t = {111, 222, 333}
for k, v in pairs(t) do
  print(k..': '..v)
end
-- 1: 111
-- 2: 222
-- 3: 333

t = {n=42, x=100, y=200}
for k, v in pairs(t) do
  print(k..': '..v)
end
-- x: 100
-- y: 200
-- n: 42

See also Edit

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.