Type Function Library (globals) Return value (varies) Revision Release 2022.3683 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.
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