Previous Page Next Page

5.8. What's Next?

In the next chapter, we discuss the Perl operators. We will cover the different types of assignment operators, comparison and logical operators, arithmetic and bitwise operators, how Perl sees strings and numbers, how to create a range of numbers, how to generate random numbers, and some special string functions.

Exercise 5: The Funny Characters

1.Write a script called foods.plx that will ask the user for his five favorite foods. The foods will be stored as a string in a scalar, each food separated by a comma.
  1. Split the scalar and create an array.

  2. Print the array.

  3. Print the first and last elements of the array.

  4. Print the number of elements in the array.

  5. Create an array slice from three elements of the food array and print the values.

2.Given the array @names=qw(Nick Susan Chet Dolly Bill), write a statement that would
  1. Replace Susan and Chet with Ellie, Beatrice, and Charles.

  2. Remove Bill from the array.

  3. Add Lewis and Izzy to the end of the array.

  4. Remove Nick from the beginning of the array.

  5. Reverse the array.

  6. Add Archie to the beginning of the array.

  7. Sort the array.

  8. Remove Chet and Dolly and replace them with Christian and Daniel.

3.Write a script called elective that will contain a hash.
  1. The keys will be code numbers—2CPR2B, 1UNX1B, 3SH414, 4PL400.

  2. The values will be course names—C Language, Intro to UNIX, Shell Programming, Perl Programming.

  3. Sort the hash by values and print it.

  4. Ask the user to type the code number for the course he plans to take this semester and print a line resembling the following:

    You will be taking Shell Programming this semester.

4.Modify your elective script to produce output resembling that appearing below. The user will be asked to enter registration information and to select an EDP number from a menu. The course name will be printed. It doesn't matter if the user types in the EDP number with upper- or lowercase letters. A message will confirm the user's address and thank him for enrolling.

Output should resemble the following:

REGISTRATION INFORMATION FOR SPRING QUARTER
Today's date is Wed Apr 19 17:40:19 PDT 2007
Please enter the following information:
Your full name: Fred Z. Stachelin

What is your Social Security Number (xxx–xx–xxxx): 004–34–1234
Your address:
   Street: 1424 Hobart St.
   City, State, Zip:  Chico, CA 95926

"EDP" NUMBERS AND ELECTIVES:
______________________________
2CPR2B | C Programming
______________________________
1UNX1B | Intro to UNIX
______________________________
4PL400 | Perl Programming
______________________________
3SH414 | Shell Programming
______________________________


What is the EDP number of the course you wish to take? 4pl400

The course you will be taking is "Perl Programming."

Registration confirmation will be sent to your address at
   1424 HOBART ST.
   CHICO, CA 95926

Thank you, Fred, for enrolling.

5.Write a script called findem that will
  1. Assign the contents of the datebook file to an array. (File is on the CD.)

  2. Ask the user for the name of a person to find. Use the built-in grep function to find the elements of the array that contain the person and number of times that person is found in the array. The search will ignore case.

  3. Use the split function to get the current phone number.

  4. Use the splice function to replace the current phone number with the new phone number, or use any of the other built-in array functions to produce output that resembles the following:

    Who are you searching for? Karen
    What is the new phone number for Karen? 530-222-1255
    Karen's phone number is currently 284-758-2857.

    Here is the line showing the new phone number:
    Karen Evich:530-222-1255:23 Edgecliff Place, Lincoln, NB 92086:7/25/53:85100

    Karen was found in the array three times.

6.Write a script called tellme that will print out the names, phones, and salaries of all the people in the datebook file. To execute, type at the command line

tellme datebook

Output should resemble the following:

Salary: 14500
Name:  Betty Boop
Phone: 245–836–8357


Previous Page Next Page