Exercise 5: The Funny Characters
Write a script that will ask the user for his five favorite foods (read from STDIN). The foods will be stored as a string in a scalar, each food separated by a comma.
- Split the scalar by the comma and create an array.
- Print the array.
- Print the first and last elements of the array.
- Print the number of elements in the array.
- Use an array slice of three elements in the food array and assign those values to another array. Print the new array with spaces between each of the elements.
Given the array @names=qw(Nick Susan Chet Dolly Bill), write a statement that would do the following:
- Replace Susan and Chet with Ellie, Beatrice, and Charles.
- Remove Bill from the array.
- Add Lewis and Izzy to the end of the array.
- Remove Nick from the beginning of the array.
- Reverse the array.
- Add Archie to the beginning of the array.
- Sort the array.
- Remove Chet and Dolly and replace them with Christian and Daniel.
Write a script called elective that will contain a hash. The keys will be code numbers—2CPR2B, 1UNX1B, 3SH414, 4PL400. The values will be course names—C Language,Intro to UNIX, Shell Programming, Perl Programming.
- Sort the hash by values and print it.
- 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.
Modify your elective script to produce output resembling the output 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 2014
- 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:
- StreetHobartSt
- CityStateZipChicoCA
“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.
Write a script called findem that will do the following:
- Assign the contents of the datebook file to an array. (The datebook file is on the CD that accompanies this book.)
- 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.
- Use the split function to get the current phone number.
- 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.
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 the following at the command line:
- tellme datebook
- Output should resemble the following:
- Salary: 14500Name: Betty BoopPhone: 245-836-8357
The following array contains a list of values with duplicates.
- @animals=qw( cat dog bird cat bird monkey elephant cat elephant pig horse cat);
- Remove the duplicates with the built-in map function.
- Sort the list.
- Use the built-in grep function to get the index value for the monkey.