WoW:API next: Difference between revisions

44 bytes added ,  15 August 2023
m
Move page script moved page API next to WoW:API next without leaving a redirect
(Added "Changing values of existing keys or removing keys is alright though.")
m (Move page script moved page API next to WoW:API next without leaving a redirect)
 
(3 intermediate revisions by 3 users not shown)
Line 1: Line 1:
Native Lua statement:
{{wowlua}}
Returns the next key/value pair for a given table and key.
key, value = next (table, [oldKey])


next (table, [index])
Allows a program to traverse all fields of a table. Its first argument is a table and its second argument is an index in this table. '''<tt>next</tt>''' returns the next index of the table and the value associated with the index. When called with nil as its second argument, '''<tt>next</tt>''' returns the first index of the table and its associated value. When called with the last index, or with nil in an empty table, '''<tt>next</tt>''' returns nil. If the second argument is absent, then it is interpreted as nil.


Allows a program to traverse all fields of a table. Its first argument is a table and its second argument is an index in this table. <b><tt>next</tt></b> returns the next index of the table and the value associated with the index. When called with nil as its second argument, <b><tt>next</tt></b> returns the first index of the table and its associated value. When called with the last index, or with nil in an empty table, <b><tt>next</tt></b> returns nil. If the second argument is absent, then it is interpreted as nil.
Lua has no declaration of fields; There is no difference between a field not present in a table or a field with value nil. Therefore, '''<tt>next</tt>''' only considers fields with non-nil values. The order in which the indices are enumerated is not specified, even for numeric indices. (To traverse a table in numeric order, use a numerical for or the '''<tt>ipairs</tt>''' function.)


 
The behavior of '''<tt>next</tt>''' is undefined if, during the traversal, you assign any value to a non-existent field in the table. Changing values of existing keys or removing keys is alright though.
Lua has no declaration of fields; There is no difference between a field not present in a table or a field with value nil. Therefore, <b><tt>next</tt></b> only considers fields with non-nil values. The order in which the indices are enumerated is not specified, even for numeric indices. (To traverse a table in numeric order, use a numerical for or the <b><tt>ipairs</tt></b> function.)
 
 
The behavior of <b><tt>next</tt></b> is undefined if, during the traversal, you assign any value to a non-existent field in the table. Changing values of existing keys or removing keys is alright though.
 
{{LUA}}
Anonymous user