Global/next

From Garry's Mod
(Difference between revisions)
Jump to: navigation, search
m (Noted that the non-guaranteed table ordering only applies to non-numerical keys)
(actually, even with numerical keys, the order isn't guaranteed. It's USUALLY ordered which makes it hard to prove, but it's not guaranteed.)
 
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|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.}}
+
{{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. If you need to iterate over an array in order, use {{GlobalFunction|ipairs}}.}}
 
|Realm=Shared and Menu
 
|Realm=Shared and Menu
 
|IsClass=No
 
|IsClass=No

Latest revision as of 13:40, 19 April 2017

 next( )

Contents

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. If you need to iterate over an array in order, use ipairs.

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