Garry's Mod Wiki

Revision Difference

Global.pairs#528421

<function name="pairs" parent="Global" type="libraryfunc"> <description> Returns an iterator function(<page>Global.next</page>) for a for loop that will return the values of the specified table in an arbitrary order. ⤶ For alphabetical **key** order use <page>Global.SortedPairs</page>.⤶ ⤶ For alphabetical **value** order use <page>Global.SortedPairsByValue</page>.⤶ * For alphabetical **key** order use <page>Global.SortedPairs</page>.<br />⤶ * For alphabetical **value** order use <page>Global.SortedPairsByValue</page>.⤶ <note>In sequential tables like <page>player.GetAll</page> or <page>ents.GetAll</page> (and `ents.Find*` functions), it's more efficient to use <page>Global.ipairs</page> or [for](http://lua-users.org/wiki/ForTutorial) iterator.</note>⤶ </description> <realm>Shared and Menu</realm> <args> <arg name="tab" type="table">The table to iterate over.</arg> </args> <rets> <ret name="" type="function">The iterator (<page>Global.next</page>).</ret> <ret name="" type="table">The table being iterated over.</ret> <ret name="" type="any">**nil** (for the constructor).</ret> </rets> </function> <example> <description>Iterates through all players on the server and prints their names.⤶ **Note: In sequential tables like <page>player.GetAll</page> or <page>ents.GetAll</page>, it's more efficient to use <page>Global.ipairs</page> instead of `pairs`.**⤶ ⤶ </description>⤶ <description>Iterates through all <page text="PlayerInitialSpawn">GM:PlayerInitialSpawn</page> hooks on the server and prints their unique identifiers and called function.</description>⤶ <code> for k, v in pairs( player.GetAll() ) do⤶ print( v:Nick() ) end⤶ for k, v in pairs( hook.GetTable().PlayerInitialSpawn )⤶ ⤶ -- The custom name given in the second argument of the hook.Add function. -- Example : "myCustomSpawnFunc".⤶ print( "Unique identifier:", k )⤶ ⤶ -- The hook function.⤶ -- Example : "function: 0x3a3f2c80".⤶ print( "Called function:", v )⤶ ⤶ end⤶ </code> <output>A list of players in console.</output> <output>A list of all hooks.</output> </example>