Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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)

like image 596
Klaider Avatar asked Jul 16 '15 11:07

Klaider


People also ask

How do you access the length of an array?

To find the length of an array, reference the object array_name. length. The length property returns an integer. You'll often want to know how many values are in the array—in other words, the length of the array.

How do you find the length of an array array?

With the help of the length variable, we can obtain the size of the array. Examples: int size = arr[]. length; // length can be used // for int[], double[], String[] // to know the length of the arrays.

How do you find the length of a Lua table?

1) Remember in Lua we do not have any function or we can say direct function to get the size or length of the table directly. 2) we need to write the code manually to get the length of the table in Lua. 3) For this we can use pair() which will iterator the table object and give us the desired result.

Which function is used to prints the length of an array?

Using sizeof() function to Find Array Length in C++ The sizeof() operator in C++ returns the size of the passed variable or data in bytes. Similarly, it returns the total number of bytes required to store an array too.


1 Answers

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.

like image 116
dlask Avatar answered Sep 22 '22 16:09

dlask