Menu
Coddy logo textTech

Searching for a Contact

Part of the Logic & Flow section of Coddy's Lua journey — lesson 45 of 54.

challenge icon

Challenge

Easy

Implement the search functionality for your contact list application. When the user enters search, the program should prompt for a name and display all matching contacts.

Building on the previous challenge, add functionality to handle the search command. Loop through the contacts table to find contacts whose names match the search term. Display all matches or inform the user if no contact is found.

Requirements:

  • Keep all code from the previous challenge (setup, menu display, main loop, add functionality, and list functionality)
  • When the user enters search, print: Enter name to search:
  • Read the search term using io.read()
  • Use ipairs() to iterate through the contacts table
  • For each contact, check if the name field matches the search term exactly
  • If a match is found, print in the format: Found: [name] - [phone]
  • If no match is found after checking all contacts, print: Contact not found.
  • The add, list, and quit commands should still work as before
  • Other commands should still print Command not yet implemented

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:
search
Enter name to search:
Alice
Found: Alice - 555-1234

Enter command:
search
Enter name to search:
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 found.")
        else
            print("Contact List:")
            for i, contact in ipairs(contacts) do
                print(i .. ". " .. contact.name .. " - " .. contact.phone)
            end
        end
        print()
        
    elseif command == "quit" then
        print("Goodbye!")
        break
        
    else
        print("Command not yet implemented")
        print()
    end
end

All lessons in Logic & Flow