Deleting a Contact
Part of the Logic & Flow section of Coddy's Lua journey — lesson 46 of 54.
Challenge
EasyImplement the delete functionality for your contact list application. When the user enters delete, the program should prompt for a name, find that contact in the list, and remove it.
Building on the previous challenge, add functionality to handle the delete command. Loop through the contacts table to find the contact with the matching name, then use table.remove() to delete it from the list. Provide feedback to the user about whether the deletion was successful.
Requirements:
- Keep all code from the previous challenge (setup, menu display, main loop, add, list, and search functionality)
- When the user enters
delete, print:Enter name to delete: - Read the name using
io.read() - Use
ipairs()to iterate through thecontactstable - For each contact, check if the
namefield matches the name to delete exactly - If a match is found, use
table.remove()with the contact's index to delete it - After successful deletion, print:
Contact deleted successfully! - If no match is found after checking all contacts, print:
Contact not found. - When the
listcommand is used and there are no contacts, print:No contacts available. - The
add,list,search, andquitcommands should still work as before
Note: The empty list message in this challenge is No contacts available. — make sure to use this exact string, even if a previous lesson used different wording.
Example interaction:
Welcome to Contact List Manager!
Available Commands:
add - Add a new contact
list - Display all contacts
search - Search for a contact
delete - Delete a contact
quit - Exit the program
Enter command:
add
Enter name:
Alice
Enter phone number:
555-1234
Contact added successfully!
Enter command:
add
Enter name:
Bob
Enter phone number:
555-5678
Contact added successfully!
Enter command:
list
Contact List:
1. Alice - 555-1234
2. Bob - 555-5678
Enter command:
delete
Enter name to delete:
Alice
Contact deleted successfully!
Enter command:
list
Contact List:
1. Bob - 555-5678
Enter command:
delete
Enter name to delete:
Charlie
Contact not found.
Enter command:
quit
Goodbye!Try it yourself
-- Contact List Manager
local contacts = {}
-- Display welcome message
print("Welcome to Contact List Manager!")
print()
-- Display menu
print("Available Commands:")
print("add - Add a new contact")
print("list - Display all contacts")
print("search - Search for a contact")
print("delete - Delete a contact")
print("quit - Exit the program")
print()
-- Main loop
while true do
print("Enter command:")
local command = io.read()
if command == "add" then
print("Enter name:")
local name = io.read()
print("Enter phone number:")
local phone = io.read()
table.insert(contacts, {name = name, phone = phone})
print("Contact added successfully!")
print()
elseif command == "list" then
if #contacts == 0 then
print("No contacts available.")
else
print("Contact List:")
for i, contact in ipairs(contacts) do
print(i .. ". " .. contact.name .. " - " .. contact.phone)
end
end
print()
elseif command == "search" then
print("Enter name to search:")
local searchName = io.read()
local found = false
for i, contact in ipairs(contacts) do
if contact.name == searchName then
print("Found: " .. contact.name .. " - " .. contact.phone)
found = true
break
end
end
if not found then
print("Contact not found.")
end
print()
elseif command == "quit" then
print("Goodbye!")
break
else
print("Command not yet implemented")
print()
end
endAll lessons in Logic & Flow
1Advanced Table Iteration
Iterating with pairs()Iterating with ipairs()pairs() vs. ipairs()Recap - Character Sheet2More Table Library Functions
table.concat()table construction & unpack()table.sort()Custom Sorting with FunctionsRecap - High Score Board5Project: Text Adventure Engine
Project Setup: The RoomLinking Rooms8Project: Contact List
Project SetupThe Main Loop