ASSIGMENT 4 Basic Perl operations -- Perl file handles Due: Friday, Feb. 22, 23:59:59pm STATUS: DRAFT -- MAY BE REVISED Create 2 Perl programs that accept user input as specified below. Program 1: Program 1 deals with basic Perl operations including subroutine calls. When your program starts up, it should create in memory: 1. two variables with numeric data 2. an array containng three to five elements of string or numeric data 3. a hash table of three to five key/value pairs Your program has to work with these variables and perform the following actions when the user enters the following commands (all input is in lower case): add add the two numeric variables and print the calculation and result to screen, like: 3 + 2 = 5 subtract subtract the two numeric variables and print the calculation and result to screen, like: 3 - 2 = 1 print array print each element of the array to screen, separated by tabs or newline characters pop delete the last element of the array and print the deleted element on screen shift delete the first element of the array and print the deleted element on screen -------------------------------------------------------------------- NOTE: If your array is empty, the output for the preceding three commmands must notify the user with something like: "Array is empty." -------------------------------------------------------------------- print hash print the key/value pairs of your hash to screen; each pair should appear on a separate line, key and value separated by a space, tab, or other characters t hat make their relationship clear, like: key --> value hash keys print each key of the hash to screen, separated by tabs or newline characters hash values print each value of the hash to screen, separated by tabs or newline characters delete delete the first or last element of the hash and print the deleted key/value pair on screen --------------------------------------------------------------------- NOTE: If your hash is empty, the output for the preceding three commmands must notify the user, something like: "Hash is empty." --------------------------------------------------------------------- usage print a list of available commands on screen. You should implement the printing of this list via a subroutine call. exit quit the program PROGRAM 2: Program 2 deals with reading files, writing to files, and extracting and formatting data. When program starts up, it should create in memory two variables that contain file names: one for your source file, called "data.txt", and one for your target file, called "output.txt" The data file will be posted on the course web site. You must download this file and save it as a text file under the name "data.txt" in the same directory that contains your program 2. Important Instructions: Different computer platforms have different conventions regarding new lines. [See this link for an explanation: http://www.classes.cs.uchicago.edu/current/10100-1/html/hwks/SimonEndOfLineGloss.html ] In order to avoid problems reading the "data.txt" file in your program, do the following: Open the "data.txt" in your favorite browser. Select and copy the entire text. Open a new file in any text editor you may be using and paste the text into the new file. Save it as "data.txt" into the same directory as the program you are going to write. This will ensure that your datafile has newlines corresponding to the platform you are working on. Remember: Data file and script must be in the same directory. The data file contains a list of books. Every line represents one record, i.e. one book. Each record consists of the author name, book title, binding, price, number of pages, and the year of publication. Each of theses items is separated by a double colon, "::". Hence, "data.txt" is a double-colon delimited datafile. Your program should extract, display, reformat, and print the data on screen or to a new file according to the command specification below. (Just to clarify the meaning of the commands: All of the authors are composers (VERDI, ROSSINI, DONIZETTI), except one, VAN GULIK, who is an author of mystery novels. Publishers often "remainder" old books that don't sell anymore. These are the books that you find at greatly reduced prices at the entrance or cash register of Barnes & Noble.) show file open the data file (this is the file whose name you have stored in your source file variable), read it, and display the content "as is" on screen show music open the data file, read it, but display only the books of the composers on screen "as is" show mystery open the data file, read it, but display only the books of the mystery writer on screen "as is" show remainder open the data file, read it, but display on screen only books published before 1992; IN ADDITION, reformat your output that shows only the author name, title, and manner: AUTHOR: TITLE, pub year create verdi open the data file, read it. If the author name is VERDI, reformat the record to show author, title, and publication year, and write the record to a new file (this is the file whose name you have stored in your target file variable). The format of this file must be a tab-delimited datafile, that is, the three data fields of the record must be delimited by a tab character, like: AUTHORtabTITLEtabPUBYEAR. When the command is executed, print a message to screen indicating that the file has been created. add rossini do the same as in 'create verdi' for the author name ROSSINI. However, do NOT create a new file, but APPEND the output to the existing output file usage print a list of available commands on screen exit quit the program IMPORTANT: ADDITIONAL CONDITIONS FOR BOTH PROGRAMS 1. Once your progam has created an 'output.txt' file, change its permissions. Type on the command line: chmod 666 output.txt This will ensure that the grader can run your program and write to the output file. 2. All commands must be implemented and your program may not crash at any point. If it does crash, you will receive only ONE THIRD of the available credit for this problem, no matter how much work you've done. (If both programs crash, you'll get a D+.) We will test your program under reasonable circumstances; we will not try to make it crash intentionally. 3. In order to make sure that the program doesn't crash, your program must know how to handle input for which you haven't specified conditions, for example, when the user mistypes a command or uses the wrong case. You should present the user with an error message. 4. If, for some inexplicable reason, you are not able to complete the entire assignment, it is better if you present the user (i.e., the grader) with a message saying, "Feature x has not yet been implemented". This will carry a lesser deduction than an error message. Therefore, when you start working on your assignment, do the commmand responses first. Then, when you've provided responses for all commands, begin to implement the functionality. We also suggest that you work on the problems in the order given here. EVEN MORE IMPORTANT: Read these instructions carefully and repeatedly. We will demonstrate the functionality of these programs in class. If you are not entirely sure what these instructions mean, COME TO CLASS and ask. Take advantage of your resources, especially the online Perl reference chapters via the course schedule page or at: http://www.classes.cs.uchicago.edu/current/10100-1/html/perl/tocPerlRef.html