lua

table.getn is deprecated - How can I get the length of an array?


I'm trying to get the length of an array in Lua with table.getn. I receive this error:

The function table.getn is deprecated!

(In Transformice Lua)


Solution

  • Use #:

    > a = {10, 11, 12, 13}
    > print(#a)
    4
    

    Notice however that the length operator # does not work with tables that are not arrays, it only counts the number of elements in the array part (with indices 1, 2, 3 etc.).

    This won't work:

    > a = {1, 2, [5] = 7, key = '1234321', 15}
    > print(#a)
    3
    

    Here only (1, 2 and 15) are in the array part.