Computer Sciences Department logo

CS 368-1 (2011 Summer) — Day 3 Homework

Due Friday, July 15th, 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.

% ./homework-03-script

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

Think about how the user will 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.

Extra Challenges

If the requirements above were easy, try one or more of the following challenges! Sorry, there is no such thing as extra credit, though.


Do the work yourself, consulting reasonable reference materials as needed; any reference material that gives you a complete or nearly complete solution to this problem or a similar one is not OK to use. Asking the instructors for help is OK, asking other students for help is not.

Hand In

A printout of your code on a single sheet of paper (if at all possible). Be sure to put your own name in the initial comment block of the code. Identifying your work is important, or you may not receive appropriate credit.