Global/next

From Garry's Mod
(Difference between revisions)
Jump to: navigation, search
(Added an example: IsEmptyTable())
m (Noted that the non-guaranteed table ordering only applies to non-numerical keys)
Line 2: Line 2:
 
|Description=Returns the next key and value pair in a table.
 
|Description=Returns the next key and value pair in a table.
  
{{Note|Table keys in Lua have no specific order, and will be returned in whatever order they exist in memory. This may not always be in ascending order or alphabetical order.}}
+
{{Note|Non-numerical keys in Lua have no specific order, and will be returned in whatever order they exist in memory. This may not always be in ascending order or alphabetical order.}}
 
|Realm=Shared and Menu
 
|Realm=Shared and Menu
 
|IsClass=No
 
|IsClass=No

Revision as of 07:35, 19 April 2017

 next( )

Contents

Description

Returns the next key and value pair in a table.

NOTE

Non-numerical keys in Lua have no specific order, and will be returned in whatever order they exist in memory. This may not always be in ascending order or alphabetical order.

Arguments

table tab

The table

Arguments

any prevKey=nil

The previous key in the table.

Returns

any

The next key for the table. If the previous key was nil, this will be the first key in the table. If the previous key was the last key in the table, this will be nil.

Returns

any

The value associated with that key. If the previous key was the last key in the table, this will be nil.

Examples

Example

Returns whether the table is empty or not

local function IsEmptyTable( t )
	return next( t ) == nil
end

local mytable = {}
print( "mytable is empty:", IsEmptyTable( mytable ) )
mytable["hello"]=true
print( "mytable is empty:", IsEmptyTable( mytable ) )

Output:

mytable is empty: true
mytable is empty: false
Personal tools
Navigation