JT Archie

# Lua Tables: Quick and Easy Key-Value Pairs

Lua tables are versatile data structures that can be used for various purposes, including arrays, dictionaries, and more. This guide focuses on using Lua tables as key-value pairs, providing an efficient way to store and access data.

Consider a simple example where you want to store information about a user. This is a standard key-value pair.

local user = {
  name = "John Doe",
  age = 30,
  email = "john.doe@example.com"
}

You can access the data stored in the table using the key associated with each value. This id done using the dot lookup.

print(user.name)  -- Output: John Doe
print(user.age)   -- Output: 30
print(user.email) -- Output: john.doe@example.com

You can also add new key-value pairs to the table or update existing ones.

By using Lua tables as key-value pairs, you can easily store, access, and modify data in a structured and readable way. This can lead to better code organization and improved code readability in your Lua projects.

user.phone = "555-1234"          -- Add a new key-value pair
user.email = "john@example.com"  -- Update the existing value

print(user.phone) -- Output: 555-1234
print(user.email) -- Output: john@example.com

# Follow for more tips