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

table.foreachi

Summary

Applies a function to each item in a numerically-keyed table

Prototype

val = table.foreachi (t, f)


Description

Executes f for each element in table t.

Function f is called with the arguments (key, value).

If f returns a non-nil value the loop is broken, and this value is returned as the result from table.foreach. Effectively this could be used to find an element inside a table matching a certain condition.

Similar to table.foreach, except that only numeric keys in the range 1 to n are processed.


t = { "the", "quick", "brown", "fox", name = 'Nick' }
table.foreachi (t, print)

 -->

1 the
2 quick
3 brown
4 fox


In this example the entry for "name = 'Nick'" was not returned because it did not have a numeric key.


Warning the use of table.foreachi is deprecated. This means it may not be available in future versions of Lua. You are recommended to rewrite such uses by using the 'ipairs' function. For example:


for k, v in ipairs (t) do
  f (k, v)
end 


See Also ...

Lua functions

table.concat - Concatenates table items together into a string
table.foreach - Applies a function to each item in a table
table.getn - Returns the size of a numerically-keyed table
table.insert - Inserts a new item into a numerically-keyed table
table.maxn - Returns the highest numeric key in the table
table.remove - Removes an item from a numerically-keyed table
table.setn - Sets the size of a table (obsolete)
table.sort - Sorts a table

Topics

Lua base functions
Lua bc (big number) functions
Lua bit manipulation functions
Lua coroutine functions
Lua debug functions
Lua io functions
Lua LPEG library
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
Scripting callbacks - plugins

(Help topic: lua=table.foreachi)

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.

[Home]


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 FutureQuest]