Garry's Mod Wiki

next

  any, any next( table tab, any prevKey = nil )

Description

Returns the next key and value pair in a table.

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

1 table tab
The table
2 any prevKey = nil
The previous key in the table.

Returns

1 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.
2 any
The value associated with that key. If the previous key was the last key in the table, this will be nil.

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