[Home] [Downloads] [Search] [Help/forum]

Register forum user name Search FAQ



Iterates over a numerically keyed table


it, t, 0 = ipairs (t)


Returns an iterator function, the table t, and 0, for use in the generic "for" loop.

for i, v in ipairs (t) do
  -- process loop here (i is the key, v is the value)
end -- for

The iterator function, called repeatedly, returns the key and value pairs for each table item, starting at 1, until it finds the first missing integer key. For example, if keys 1 to 8 are present, and key 9 is nil, it will return the first 8 values. This occurs even if key 10 is present.

In other words, you cannot use ipairs to iterate over a table with "holes" in the key ranges.

If the table has non-numeric keys, there are gaps in the key sequence, or keys do not start at 1, then you need to use pairs (instead of ipairs) to access every item in the table (however this access will not necessarily be in numeric sequence).

Most usually you simply use ipairs to traverse a numerically-keyed table, for example:

t = {

for key, value in ipairs (t) do
  print (key, value)
end -- for

Example output:

1 the
2 quick
3 brown
4 dog

In this case the keys (automatically assigned) were 1, 2, 3, 4, and the values were the words in the table.

See Also ...

Lua functions

assert - Asserts that condition is not nil and not false
collectgarbage - Collects garbage
dofile - Executes a Lua file
error - Raises an error message
gcinfo - Returns amount of dynamic memory in use
getfenv - Returns the current environment table
getmetatable - Returns the metatable for the object
load - Loads a chunk by calling a function repeatedly
loadfile - Loads a Lua file and parses it
loadlib - Loads a DLL (obsolete in Lua 5.1)
loadstring - Compiles a string of Lua code
module - Creates a Lua module
next - Returns next key / value pair in a table
pairs - Traverse all items in a table
pcall - Calls a function in protected mode
print - Prints its arguments
rawequal - Compares two values for equality without invoking metamethods
rawget - Gets the value of a table item without invoking metamethods
rawset - Sets the value of a table item without invoking metamethods
require - Loads a module
select - Returns items in a list
setfenv - Sets a function's environment
setmetatable - Sets the metatable for a table
tonumber - Converts a string (of the given base) to a number
tostring - Converts its argument to a string
type - Returns the type of a variable
unpack - Unpacks a table into individual items
xpcall - Calls a function with a custom error handler


Lua base functions
Lua bc (big number) functions
Lua bit manipulation functions
Lua coroutine functions
Lua debug functions
Lua io functions
Lua math functions
Lua os functions
Lua package functions
Lua PCRE regular expression functions
Lua script extensions
Lua string functions
Lua syntax
Lua table functions
Lua utilities
Scripting callbacks - plugins

(Help topic: lua=ipairs)

Documentation contents page

Search ...

Enter a search string to find matching documentation.

Search for:   

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at HostDash]