Your program will process entries in an array. Each entry in the array 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 that you will use:
@phonelist = ( " 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", "Ratchett, Doofus P. ; 408 375 - 27651" );
You will add several items to the array. You will put these into the array, and not prompt the user to enter them! You must enter the additional items according to the following rules. Names come in two forms:
Dijkstra, Edsger W. Hoare, C. Anthony R. Peterson 3rd, Gordon E.
Evergreen Valley College O'Reilly & Associates Key Point Software
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. becomes Edsger W. Dijkstra Hoare, C. Anthony R. becomes C. Anthony R. Hoare Peterson 3rd, Gordon E. becomes Gordon E. Peterson 3rd Key Point Software remains Key Point Software Evergreen Valley College remains Evergreen 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. In the example list above, notice that the number for "Ratchett, Doofus P." is incorrect. Do not do a check against the person’s name to find out whether to ignore a phone number! Check the phone number!
274-7900 becomes (408) 274-7900 510-555-0297 becomes (510) 555-0297 312 555-8763 becomes (312) 555-8763 (800)775-7731 becomes (800) 775-7731
Your output should be a list sorted by phone number. Include only entries with valid phone numbers. Here is an example of what the output 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
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
Hapsburg-Hohenzollern, Teresita
Truman, Harry S
Adair, John "Red"
...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?
Name your file lastname_firstname_regex.pl and email it to the instructor.