19

I'm trying to learn the ropes on Lua, and I was going through the online tutorials. One problem I tried to solve was to examine a table local foo = {} to see how many elements it had. The tutorial gave the suggestion to use local length = table.getn(foo). When i try this using Lua52, I get an error stating attempt to call field 'getn' (a nil value). I looked around further and noticed that any of the functions given with table produce the same type of error. Was the table library removed from Lua? Is it a third-party library, or what gives?

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
Piotr
  • 541
  • 4
  • 19

2 Answers2

41

Use the length operator # as in #foo.

table.getn was deprecated in 5.1 and removed in 5.2.

lhf
  • 70,581
  • 9
  • 108
  • 149
  • 9
    The first hit for "lua size of array" is [this obviously horribly outdated book](https://www.lua.org/pil/19.1.html) advocating `table.getn`. And thats on `lua.org`. – Zulan Sep 11 '16 at 22:00
5

The table library wasn't removed, as it's an essential part of the language and the module system. The getn function was removed but if none of the table functions work, it's almost certainly because you've overwritten table.

Craig
  • 121
  • 2
  • 2