CIT 042 Index > Program 4

Programming Assignment 4
Pattern Matching

Your program will read lines from a file. Each line of the file has one name and one phone number, separated by a semicolon. The name and phone number could have leading and trailing blanks. Here is the file you will copy and paste into a data file named phonelist.txt. This file will reside in the same directory as your Perl program.

   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

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

Phone numbers come in several 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.

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 list sorted by phone number, which will be written to a file named sortedlist.txt. 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

Note:

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.

Hints:

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.