Global/next

From Garry's Mod
(Difference between revisions)
Jump to: navigation, search
m (Also menu state)
(Added an example: IsEmptyTable())
Line 5: Line 5:
 
|Realm=Shared and Menu
 
|Realm=Shared and Menu
 
|IsClass=No
 
|IsClass=No
|Name=next
 
|Parent=Global
 
 
}}
 
}}
 
{{Arg
 
{{Arg
Line 26: Line 24:
 
|type=any
 
|type=any
 
|desc=The value associated with that key. If the previous key was the last key in the table, this will be nil.
 
|desc=The value associated with that key. If the previous key was the last key in the table, this will be nil.
 +
}}
 +
{{Example
 +
|Description=Returns whether the table is empty or not
 +
|Code=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 ) )
 +
|OutputFixedWidth=Fixed width
 +
|Output=mytable is empty: true
 +
mytable is empty: false
 
}}
 
}}

Revision as of 14:44, 7 January 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.

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