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

The pairs() iterator function is used exclusively with 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
  -- ...

This iterator produces every value in the table, not just those with sequential indices. It can produce keys and values for tables being used as mappings (dictionaries) or objects.

Be warned that results are not produced in any guaranteed order. Some small tables may seem to iterate in order, but that is purely coincidental behavior which should not be relied upon.

Examples[edit | edit source]

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

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

See also[edit | edit source]

Community content is available under CC-BY-SA unless otherwise noted.