Programming in Perl, Summer 98
Homework 1
Due : Thursday, May 28 at midnight
Weight : 10% of course grade
Instructor : Moorthy

This assignment consists of a series of small Perl programs. Don't forget to comment your program, providing a general description of the Perl script along with inline comments for important or complex portions of code.

Problem 1 Data Mining a Dictionary Write Perl scripts that find and print the following items in a dictionary (dictionary is usually at /usr/dict/words.

  1. Longest Word in a dictionary
  2. Last Word in a dictionary
  3. Given an argument i, finds the ith word in the dictionary.
  4. Display a histogram for the occurences of lower case alphabets in the dictionary.
  5. Display a histogram on the lengths of words.
  6. Prints the dictionary in reverse order.
  7. Prints the dictionary in increasing order of lengths of words.
  8. Histogram x value should be output one to a line. Each line should begin with the alphabet/integer followed by a space, a colon, and another space. The number of items occuring for the x-value should be printed as an integer.

    Problem 2 A small mail-order book store uses the price codes and corresponding prices as given below on the left and charges shipping and handling based on the total number of items ordered as indicated below on the right:

    Code Price   Books Charge
    CL1 12.98   1-3 3.49
    HR1 11.98   4-6 6.49
    CL2 10.98   7-9 8.99
    SR1 11.98   10+ 10.99
    SR2 14.98      
    SR3 24.98      

    Store the price information as a hash called %code_to_price using the price codes as keys and the prices as values. The shipping and handling charges should be stored in an array. A nested if structure can be used to select appropriate elements of the array when calculating shipping and handling.

    Write a Perl script book.plx that processes a customer's order. The script should first prompt for a customer's account number. You may assume any account number entered is valid. The program should then display the following menu:

    P -- Place a book on order.
    R -- Remove a book from on order.
    T -- Total bill and list bookss ordered.
    E -- Exit.
    Type choice and hit enter:
    
    The script should respond correctly to either a lower or uppercase character. The actions to be taken for each choice are as follows:

    P
    • prompt the user to enter a code for the book to be purchased
    • read the book code input (you may assume any code entered is valid)
    • prompt the user to enter a price code
    • read the price code input
    • use the price code entered to index the %code_to_price hash. If the corresponding value is defined, use the book code as a key to a hash called %purchase and store the price as a value of %purchase. If there is no value in %code_to_price for the price code entered, print a message that an invalid price code was input and do not modify the %purchase hash (do not reprompt; display the main menu again).

    R
    • prompt the user to enter a code for the BOOK to be purchased
    • read the book code input (you may assume any code entered is valid)
    • if there is no value corresponding to the book code in %purchase, print an appropriate message (do not reprompt); otherwise, delete the location in %purchase associated with the book code entered

    T
    • output a line indicating the customer's account number
    • output a header for two columns that will be printed; it should consist of ``BOOK code'', two tabs, and ``Price''
    • print the book code and price for each order stored in %purchase (separate them by two tabs)
    • use each price to total the charges for the customer. In addition, count the number of items purchased.
    • use the number of items purchased to determine shipping and handling charges and add this to the total bill (ignore possible taxes)
    • print the number of items purchased and the total cost to the customer
    • some example output:
      Purchases for account number M21C:
      BOOK code         Price 
      CL3456          $12.98
      CL3367          $10.98
      SR2345          $11.98
      SR8889          $24.98
      Total items on order: 4
      Total due (includes shipping and handling): $67.41
      

    other
    • indicate that the choice entered was invalid

    The script should continue prompting for choices until an 'E' or 'e' is input by the user.



    Recall from the first lecture that in order to run a Perl script, you need to do the following:

    1.
    place #!/usr/local/bin/perl -w at the beginning of the script file on a line by itself (assume the file is named script.plx)

    2.
    execute the following command at a UNIX prompt: chmod a+x script.plx

    3.
    run the script by typing the following at a UNIX prompt: script.plx



    Moorthy
    5/21/1998