Lua lets you do more with tables than just holding lists of values. It also allows us to map data from a key to a value. Whereas a list uses numbers to access the values it contains, a map uses a key (often a string value).
Here is a program that creates a map of student names to email addresses, then
loops over the table printing out each one. Note the use of pairs in the for loop
not ipairs as we did with the list.
-- More with tables.
local students = { Alice = "alice@work.com", Bob = "bob@home.com", Carol = "carol@cafe.com" }
for k, v in pairs(students) do
io.write("Key " .. k .. " has email address " .. v .. "\n")
end
io.write("\n")
The output of this program is as follows:
Key Alice has email address alice@work.com
Key Carol has email address carol@cafe.com
Key Bob has email address bob@home.com
The output of this program also shows us that ordering of entries in a map are not preserved. Here Bob's information is printed out last, this may or may not be the case on your machine.
We can modify the values in a table using the same square brackets [] notation
we used for lists. Only this time we use string values not numbers. Take a look
at this program that modifies an entry then adds a new one.
-- More with tables.
local students = { Alice = "alice@work.com", Bob = "bob@home.com", Carol = "carol@cafe.com" }
students["Alice"] = "alice@home.com" -- Set an existing key to a new value.
students["Dave"] = "dave@school.com" -- Creates a new key and value.
for k, v in pairs(students) do
io.write("Key " .. k .. " has email address " .. v .. "\n")
end
io.write("\n")Key Alice has email address alice@home.com
Key Dave has email address dave@school.com
Key Carol has email address carol@cafe.com
Key Bob has email address bob@home.comWhat if we want to store more than just an email address for each student? Maybe we want a set of contact information for them. A table is just another type of value, so can be stored in a table creating a nested table. The following program maps a name (the string "Alice") to a table of contact information.
-- Nested tables.
local students = {}
local alice_info = { Email = "alice@home.com", Phone = "555-4321" }
students["Alice"] = alice_info
io.write("Alice's phone number is " .. students["Alice"]["Phone"] .. "\n")
Output of this program is Alice's phone number is 555-4321.
A more complete example of this is shown below. We don't need to assign the contact information to a variable before putting it in the table. We can instead use a nested table literal.
-- Nested tables and looping.
local students = {
Alice = { Email = "alice@home.com", Phone = "555-4321" },
Bob = { Email = "bob@work.com", Phone = "555-1234" }
}
for student, contact_details in pairs(students) do
io.write(student .. ":\n")
for key, value in pairs(contact_details) do
io.write("\t" .. key .. ": " .. value .. "\n")
end
endBob:
Email: bob@work.com
Phone: 555-1234
Alice:
Email: alice@home.com
Phone: 555-4321
Write a program that creates a map between student names (string values) and
contact information (tables). The contact information should contain a key
called Address that itself maps to another table with the lines of a student's
address in it. Try designing this with pen and paper first before coding it.
Write a for loop to print out each student name and their address.
local students = {
Alice = {
Email = "alice@home.com",
Phone = "555-4321",
Address = {
Number = 100,
Street = "Fake st.",
City = "Big Apple"
}
},
Bob = {
Email = "bob@work.com",
Phone = "555-1234",
Address = {
Number = 200,
Street = "Real Ave.",
City = "Windy City"
}
}
}
for student, contact_details in pairs(students) do
io.write(student .. ":\n")
for key, value in pairs(contact_details["Address"]) do
io.write("\t" .. key .. ": " .. value .. "\n")
end
end