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)
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With