table.getn

From GMod Wiki

Revision as of 21:18, 2 November 2009 by Crazy Quebecer (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Lightbulb.gif This function is deprecated in Lua version 5.1. Please use the hash ('#') operator to get the size of a table instead.


Function
Syntax table.getn( Table table )
Where is this used?
Description:
Returns the highest consecutive numerical index from 1 in a table. Returns 0 if there are no numerical 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.getn]Table.getn [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b]



Example

DescriptionPrints a few various conditions involving table.getn and a table.
Used onNewerShared.png
Code
 
//Define the table.
test_table = {"this","that","and the other"}
 
//Our table looks something like this:
/*
Index | Value
______|________________
  1   | "this"
  2   | "that"
  3   | "and the other"
*/
// Prints "3" to the console, because 3 is the highest consecutive number starting from 1.
print(table.getn(test_table))
 
//Lets add "hello" to the table
table.insert(test_table,"hello")
 
//Our table looks something like this now:
/*
Index | Value
______|________________
  1   | "this"
  2   | "that"
  3   | "and the other"
  4   | "hello"
*/
// Prints "4" to the console, because 4 is the highest consecutive number starting from 1.
print(table.getn(test_table))
 
//We set some values on 0 and -1 in our table
test_table[0] = "test"
test_table[-1] = "Negative number."
 
//Our table looks something like this:
/*
Index | Value
______|________________
  -1  | "negative number"
  0   | "test"
  1   | "this"
  2   | "that"
  3   | "and the other"
  4   | "hello"
*/
 
//Prints 4. Remember, we're starting from 1 and moving forward, so stuff on 0 and -1 aren't even considered.
print(table.getn(test_table))
 
//Lets set something on a non-numerical index
test_table["hello"] = "test"
/*
Index  | Value
_______|________________
"hello"| "test"
  -1   | "negative number"
  0    | "test"
  1    | "this"
  2    | "that"
  3    | "and the other"
  4    | "hello"
*/
 
//Again, prints 4. "hello" is not a number. Just like how 0 and negative indexes aren't considered, neither are string indexes.
print(table.getn(test_table))
 
//This may surprise you
test_table[6]="Goodbye!"
 
//Here's what our table looks like now:
/*
Index  | Value
_______|________________
"hello"| "test"
  -1   | "negative number"
  0    | "test"
  1    | "this"
  2    | "that"
  3    | "and the other"
  4    | "hello"
  -    |    -
  6    | "Goodbye!"
*/
 
//This prints 4. Remember, consecutive means "one after another".
//Notice that we have 1, 2, 3, and 4, but not 6.
//Think of this like a line of dominos. You tip over 1, which tips over 2, 3, and then 4, but when four falls, it doesn't knock over 6 because there's a gap between 4 and 6. So, the last domino to fall (the "highest consecutive numerical index") is 4!
print(table.getn(test_table))
 
OutputCommented on in the script above.


Additional Notes

See Also

Personal tools
Namespaces
Variants
Actions
Navigation
Lua Scripting
Functions
Hooks
Toolbox