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?
An item can be removed from an array with Lua's table. remove() function. This will remove the item at the specified position and move any following items down one index position.
In Lua, the table library provides functions to insert elements into the table. The insert function usually takes two arguments, the first argument is usually the name of the table from which we want to insert the element to, and the second argument is the element that we want to insert.
remove () function removes a value from an array using the index position of the value to be removed. This function removes the element at the pos position from the table. This causes other elements to shift down to close the space, if necessary. This function returns the value that we remove from the table.
Tables are the only data structure available in Lua that helps us create different types like arrays and dictionaries. Lua uses associative arrays and which can be indexed with not only numbers but also with strings except nil. Tables have no fixed size and can grow based on our need.
Use the length operator #
as in #foo
.
table.getn
was deprecated in 5.1 and removed in 5.2.
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
.
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