G.ipairs
From GMod Wiki
Function | |
Syntax |
G.ipairs( Table table ) Where is this used? |
Description: | |
Returns an iterator function for a for loop that will return the values of the specified table in sequential order. | |
Returns: | Function , Table |
Part of Library: | Global Functions |
Realm: | |
BBCode Link: | [b][url=http://wiki.garrysmod.com/?title=G.ipairs]G.ipairs [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b] |
Example
Description | Prints the name of all players. |
---|---|
Used on | |
Code | for k,v in ipairs( player.GetAll() ) do print( v:Name() ) end |
Output | Names of players. |
Additional Notes
- pairs is faster than ipairs[1][2] - but returns values in an arbitrary order. Use ipairs if order is important , pairs otherwise.
- The down fall to ipairs is that it only works on tables with integer keys. Therefore, a table like this:
local tab = { "key1" = value, "key2" = value2, [10] = value3, generic_name = value4 }
Can not be iterated over by ipairs.
- for k,v in ipairs(tab) do is equivalent to for k = 1,#tab do local v = tab[k], but is neater. However, it is far slower.
- for k = 1, #tab do local v = tab[k] is faster than both ipairs and pairs