View Single Post
09-20-19, 07:08 AM   #4
Sharparam
A Flamescale Wyrmkin
 
Sharparam's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2011
Posts: 102
Originally Posted by Nibs View Post
[...] the "#" operator doesn't count tables.
Yes it does. see for example:

Lua Code:
  1. local tables = {
  2.     {1, 2, 3},
  3.     {4, 5, 6},
  4.     {7, 8, 9}
  5. }
  6.  
  7. print(#tables) -- prints "3"

As long as the indices start at 1 and are contiguous it will count just fine. But if you have an index missing somewhere in the chain, it will stop counting there. For example:

Lua Code:
  1. local tables = {}
  2. tables[1] = {1, 2, 3}
  3. tables[3] = {4, 5, 6} -- Notice we skipped index 2
  4. tables[4] = {7, 8, 9}
  5.  
  6. print(#tables) -- prints "1"

Edit: Actually, the behaviour of the "#" operator on a non-contiguous table is undefined, so you cannot rely on the output if the table doesn't have indices in sequence.

Last edited by Sharparam : 09-20-19 at 07:13 AM. Reason: Clarify behaviour on non-contiguous indices
  Reply With Quote