CIT 042 Index > Files

Programming Assignment

Update the program you wrote for regular expressions to use file constructs.

New material is marked with a green border, or in bold green.

Your program will process entries in an array a file. Each entry in the array line in the file consists of one name and one phone number, separated by a semicolon. The name and phone number could have leading and trailing blanks. Here is the array the content of the file that you will use:

   Evergreen Valley College; 274-7900
Dijsktra, Edsger W.  ; 510-555-0297
Hoare, C. Anthony R.; 312 555-8763   
Key Point Software; 249-6625
   O'Reilly & Associates; (800)775-7731
Peterson 3rd, Gordon E.; 217-555-1212
Bad Phone Number ; 408  375 - 27651

You will add several items to the array lines to the file, according to the following rules. Names come in two forms:

Phone numbers come in several forms, and only in these forms:

Your program must pass the name to a subroutine called process_name. This subroutine will convert the name to "first-name-first" form and return that value. Your subroutine must use regular expressions! Names without a comma do not get changed. Thus:

Dijkstra, Edsger W. becomesEdsger W. Dijkstra
Hoare, C. Anthony R. becomesC. Anthony R. Hoare
Peterson 3rd, Gordon E. becomesGordon E. Peterson 3rd
Key Point Software remainsKey Point Software
Evergreen Valley College remainsEvergreen Valley College

Your program must pass the phone number to a subroutine called process_phone. This subroutine will convert the phone number as shown below. If no area code was given, use 408. Put a blank after the right parenthesis! This subroutine also must use regular expressions. If the phone number is not in the proper format, your subroutine must return the null string.

274-7900becomes (408) 274-7900
510-555-0297becomes (510) 555-0297
312 555-8763becomes (312) 555-8763
(800)775-7731becomes (800) 775-7731

Your output should be a file containing the list sorted by phone number. Include only entries with valid phone numbers. Here is an example of what the output file will look like. The columns must line up.

Phone            Name
-----            ----
(217) 555-1212   Gordon E. Peterson 3rd
(312) 555-8763   C. Anthony R. Hoare
(408) 249-6625   Key Point Software
(408) 274-7900   Evergreen Valley College
(510) 555-0297   Edsger W. Dijsktra
(800) 775-7731   O'Reilly & Associates

File Manipulation

The name of your program will be in the form Your program will get the name of the input file and output file as two arguments on the command line. For example, if your name is Chong Yi and your input file is named phonelist.txt and the output file is named sortedlist.txt, you would invoke the program with this command:

perl phonelist.txt sortedlist.txt

Your program must determine whether the output file already exists. If it does, you must prompt the user and ask if she wishes to delete the old file first. If the answer is Y, write the output file. If the answer is N, give an appropriate message and exit. If the answer is neither Y nor N, repeat the prompt until you get valid input. (You must accept the letter in either upper or lower case, possibly with leading and trailing blanks. Hint: Use pattern matching.)


Your program must not be tied to this specific data! You can’t presume that nobody will have a hyphen in their name, or that double quotes will never appear in a name, or that all people will have a middle name with a period in it. For example, your program must be able to handle companies named It's-It Ice Cream Sandwiches and The "Heimlich Maneuver" Cafe; or people named

...but again, not just these particular examples. The only thing you can count on is that your input follows the rules described above. The names you add to your file must be in addition to the examples above.


Don’t be too clever!

You do not need to do all the pattern matching for names or phone numbers with one huge pattern. It may be easier to use several if statements, each with a simple pattern match.

When processing names, don’t make a big production out of it. The rules are simple; your code should be too. Think: what distinguishes a company name from a person’s name?

When you are finished:

Name your file and email it to the instructor, along with your input file.