- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Table Type in Lua Programming
A table is a data type in Lua, which is used to implement associative arrays. These associative arrays can be used to implement different data structures like queues, maps, lists, etc.
An associative array in Lua is an array that can be indexed not only with numbers, but also with strings or any other value of the language, except nil.
Tables in Lua don't have any fixed size, and we can insert as many elements as we want in them, dynamically.
Tables in Lua are neither values nor variables; they are objects.
We can create tables by means of a constructor expression, which in its simplest form is written as {}.
Example
Let’s explore an example where we will create a table in Lua. Consider the example shown below −
a = {} k = "mm" a[k] = 11 print(a) print(a[k])
Output
table: 0x1018910 11
The variable that is assigned a table will hold a reference to the table, as seen in the above example.
It can also be seen that we create a key named k and assign it a value and then use that key inside the associative array.
We can also perform simple operations on the key before inserting the key into the associative array and assigning any value to it.
Example
Consider the example shown below as reference −
a = {} k = 20 print(a[k]) a[k] = 20 a[k] = a[k] * 2 print(a[k])
Output
nil 40
A table is always anonymous. There is no fixed relationship between a variable that holds a table and the table itself:
Example
Consider the example shown below as reference −
a = {} a["x"] = 10 b = a print(b["x"]) b["x"] = 20 print(a["x"])
Output
10 20
To Continue Learning Please Login
Login with Google