What is the # in the language Lua?

2

I'm watching this tutorial about creating a game and I came across something I did not understand about it of the language Lua.

There is an excerpt of the code where the following expression exists:

ents.objects[#ents.objects]

What does # (cerquilha) mean in this expression?

    
asked by anonymous 08.12.2015 / 18:25

2 answers

2

It is the length operator, it is the length . in this case you are getting the last element of array ents.objects . Remember that the array starts at 1 instead of 0. It is accessed by the% method __len

    
08.12.2015 / 18:33
2

Use to get the size of a table. Remembering that this may not work as expected because the moon table is a structure that, underneath the cloths, is implemented in a hybrid way that contains a hash table part and an array part. The # will get the size of the array part, and it is only possible to be sure that it will return the right size when a table is used as an array, ie: the table must only have numeric indexes, starting from 1, without "holes" in the middle . Otherwise, to get the amount of elements, it is preferable to iterate and count.

    
13.01.2016 / 12:12