pairs()

Type Function
Library (globals)
Return value (varies)
Revision Release 2022.3683
Keywords table, ipairs, pairs, next, iteration, loops
See also ipairs()
next()

Overview

Returns three values: the next function, a table, or nil. So, the following construction will iterate over all key-value pairs of table t.

for k,v in pairs( t ) do

end

See function next() for the caveats of modifying the table during its traversal.

Syntax

pairs( t )
t (required)

Table. The table you want to iterate over for the key-value pairs.

Example

local keyTable = { apple="red", banana="yellow", lime="green", grape="purple" }

for k,v in pairs( keyTable ) do
    print( "KEY: "..k.." | ".."VALUE: "..v )
end

--> KEY: apple | VALUE: red
--> KEY: grape | VALUE: purple
--> KEY: lime | VALUE: green
--> KEY: banana | VALUE: yellow