Type Function Library (globals) Return value (varies) Revision Release 2024.3703 Keywords table, ipairs, pairs, next, iteration, loops See also ipairs() next()
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.
pairs( t )
Table. The table you want to iterate over for the key-value pairs.
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