Computer Sciences Department logo

CS 368-3 (2012 Summer) — Day 3 Homework

Due Thursday, June 28, at the start of class.


Write a Perl script that maintains a simple grocery list with prices.


The script will let the user enter and modify a grocery list. Each item in the grocery list is associated with its price. For example, after entering a few items, the grocery list might be:

ice cream6.15

The script should cycle through the following steps:

  1. Display a list of the current grocery items and prices
  2. Display the current total cost of the list
  3. Ask the user for a grocery item and then a price
  4. Store the item and price in the list

A typical interaction might look like the following. The exact formatting is not required. In this sample interaction, the yellow highlighting shows what the user typed.


Item? milk
Price? 3.25

milk ($3.25)

Item? bread
Price? 2.99

bread ($2.99)
milk ($3.25)

Item? ice cream
Price? 6.15

bread ($2.99)
ice cream ($6.15)
milk ($3.25)
TOTAL COST: $12.39

How will the user end the script? Maybe if the item is the empty string? Maybe a special item or price that tells the script to quit? Pick something and implement it.

The program should not attempt to save its state. When the program is re-run later, it will start with an empty grocery list again.

Tip: If the user enters the same item a second time, it should replace the original item; that is, you do not need to check to see if an item already exists, just store the data.

Tip: If the user tries to enter the same item more than once, but spells it differently, it will end up being a separate item. That is, your script does not need to be clever or fancy about item names — just accept what the user types.


Testing your code is very important! If you do not run your code, you will make mistakes and probably not receive full credit. And even just running your code once may not be enough. Try different cases and make sure things work as you expect.

Here are some specific tests to consider:

Extra Challenges

If the requirements above were easy, try one or more of the following challenges. No extra credit, just extra learning!


Start your script the right way! Here is a suggestion:

# Homework for CS 368-3
# Assigned on Day 03, 2012-06-25
# Written by Your Name

use strict;
use warnings;

Do the work yourself, consulting reasonable reference materials as needed. Any resource that provides a complete solution or offers significant material assistance toward a solution not OK to use. Asking the instructor for help is OK, asking other students for help is not. All standard UW policies concerning student conduct (esp. UWS 14) and information technology apply to this course and assignment.

Hand In

A printout of your code, ideally on a single sheet of paper. Be sure to put your own name in the “<Your Name>” part of the code. Identifying your work is important, or you may not receive appropriate credit.