table.maxn

From GMod Wiki

Jump to: navigation, search
Function
Syntax table.maxn( Table )
Where is this used?
Description:
Returns the highest positive numerical index. Returns 0 if there are no numerical indexes (or only negative indexes) in the table. See examples for a simplified explanation.
Returns: Number
Part of Library: Table
Realm: NewerShared.png
BBCode Link: [b][url=http://wiki.garrysmod.com/?title=Table.maxn]Table.maxn [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b]



Example

DescriptionPrints a few various conditions involving table.maxn and a table.
Used onNewerShared.png
Code
 
//Define an empty table
test_table={}
 
//Prints 0 since this is a blank table
print(table.maxn(test_table))
 
 
//Lets put a negative index in
test_table[-1]="test";
 
//Our table looks like this now:
/*
Index   |   Value
________|__________
  -1    | "test"
*/
//Still prints 0, since we only have negative indexes
print(table.maxn(test_table))
 
 
//So lets add a positive index
test_table[3]="again";
 
//Our table looks like this now:
/*
Index   |   Value
________|__________
  -1    | "test"
   -    |    -
   -    |    -
   -    |    -
   3    | "again"
*/
//Prints 3 now that we have a positive numerical index
print(table.maxn(test_table))
 
//Lets set a positive number before 3
test_table[1]="here I am";
 
//Our table looks like this now:
/*
Index   |   Value
________|__________
  -1    | "test"
   -    |    -
   1    | "here I am"
   -    |    -
   3    | "again"
*/
 
//Still prints 3, because 3 is the highest positive numerical index in the table.
print(table.maxn(test_table))
 
 
OutputSee above.


Additional Notes


See Also

Personal tools
Namespaces
Variants
Actions
Navigation
Lua Scripting
Functions
Hooks
Toolbox