Difference between revisions of "Next"
From GiderosMobile
(removed language stuff) |
|||
(3 intermediate revisions by 2 users not shown) | |||
Line 4: | Line 4: | ||
=== Description === | === Description === | ||
− | Allows a program to traverse all fields of a table. | + | Allows a program to traverse all fields of a table. |
+ | <syntaxhighlight lang="lua"> | ||
+ | next(table,index) | ||
+ | </syntaxhighlight> | ||
− | + | Its first argument is a table and its second argument is an index in this table. ''next'' returns the next index of the table and its associated value. | |
− | The behavior of next is undefined if, during the traversal, you assign any value to a non-existent field in the table. You may however modify existing fields | + | *when called with nil as its second argument, ''next'' returns an initial index and its associated value |
− | + | *when called with the last index, or with nil in an empty table, ''next'' returns nil | |
− | + | *if the second argument is absent, then it is interpreted as nil. In particular, you can use ''next''(t) to check whether a table is empty. | |
− | + | ||
+ | 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 [[ipairs]] function. | ||
+ | |||
+ | The behavior of next is undefined if, during the traversal, you assign any value to a non-existent field in the table. You may however modify existing fields, in particular, you may clear existing fields. | ||
=== Parameters === | === Parameters === | ||
'''table''': (table) table to traverse<br/> | '''table''': (table) table to traverse<br/> | ||
'''index''': (number) previous table index '''optional'''<br/> | '''index''': (number) previous table index '''optional'''<br/> | ||
+ | |||
+ | === See also === | ||
+ | '''https://luau.org/library#global-functions'''<br/> | ||
{{(global)}} | {{(global)}} |
Latest revision as of 12:20, 21 July 2025
Available since: Gideros 2011.6
Class: (global)
Description
Allows a program to traverse all fields of a table.
next(table,index)
Its first argument is a table and its second argument is an index in this table. next returns the next index of the table and its associated value.
- when called with nil as its second argument, next returns an initial index and its associated value
- when called with the last index, or with nil in an empty table, next returns nil
- if the second argument is absent, then it is interpreted as nil. In particular, you can use next(t) to check whether a table is empty.
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 ipairs function.
The behavior of next is undefined if, during the traversal, you assign any value to a non-existent field in the table. You may however modify existing fields, in particular, you may clear existing fields.
Parameters
table: (table) table to traverse
index: (number) previous table index optional
See also
https://luau.org/library#global-functions