pairs

Traverse all items in a table

Prototype

next, t, nil = pairs (t)

Description

Returns the 'next' function, the table t, and nil, for use in a for loop.

t = { "hello", "world" }

for k, v in pairs (t) do
  print (k, v)   -- k is the key, v is the value
end 
Compare to ipairs which only traverses numerically-keyed entries.

Use the pairs function to traverse any table. The table will not necessarily be accessed in alphabetic (or any particular) order.

You must not add additional entries to the table during traversal. Doing so is undefined (that is, the script may crash or loop).

You are permitted to delete items from the table during traversal. In other words, if you find an item you want to delete you may assign nil to it, to delete it.

Lua functions

Topics