Perl Exercises

  1. Write a program that prints its own source code.
  2. Write a program that prints an array that uses the range operator. The left value should be AA and the right value should be BB. What happens and why?
  3. Using the range operator (..), create an array with the following elements: 1, 5, 6, 7, 10, 11, 12. Create a print command to display the first three and the last three elements
  4. Create an array called @months. It should have 12 elements in it with the names of the months represented as strings. Create a print command to display the last three elements.
  5. Create an associative array that holds a list of five music artists and a rating for them. Use "good," "bad," and "indifferent" as the ratings
  6. Using the range operator (..), create an array with the following elements: 1, 5, 6, 7, 10, 11, 12
  7. A word frequency counter. How often does each word show up in an array of words? Print out a report. (Hint: Use a hash to count of the number of appearances of each word.)
  8. Extend exercise 7 to count words in a text file.
  9. Write a program accepts two arguments, the first is the input file, and the second is the output file. Reverse each line of the input file and write the result to the output file. The program abort if it receives incorrect number of arguments.
  10. This example produces a score summary report by combining data from a simple file of student info and a file of their scores. A student record file has the following format: Student ID, Name, Year (delimited with colons).
  11. 123456:Washington,George:SR
    246802:Lincoln,Abraham "Abe":SO
    357913:Jefferson,Thomas:JR
    212121:Roosevelt,Theodore "Teddy":SO

    The student exam record file has the following format: Student ID, Exam number, Score on exam. Note that Abe is missing exam 2: (it is delimited with blanks).

    123456 1 98
    212121 1 86
    246802 1 89
    357913 1 90
    123456 2 96
    212121 2 88
    357913 2 92
    123456 3 97
    212121 3 96
    246802 3 95
    357913 3 94

    The desired report:


    Stu-ID


    Name...


    1


    2


    3


    Totals:


    357913


    Jefferson,Thomas


    90


    92


    94


    276


    246802


    Lincoln,Abraham "Abe"


    89

     


    95


    184


    212121


    Roosevelt,Theodore "Teddy"


    86


    88


    96


    270


    123456


    Washington,George


    98


    96


    97


    291

    Put the two records into two text files and write a program to produce the desired output as shown above. Display outputs sorted by the first exam results and total scores.

  12. You have a file called
  13. dictionary.txt that contains dictionary definitions, one per line, in the format ``word space definition''. (Here's a sample) Write a program that will look up a word from the command line. (Hints: @ARGV is a special array that contains your command line arguments and you'll need to use the three-argument form of split().) Try to enhance it so that your dictionary can also contain words with multiple definitions in the format ``word space definition:alternate definition:alternate definition, etc...''.
  14. Write a pattern that matches either "top" or "topgun".
  15. Write a program that accepts input from STDIN and changes all instances of the letter a into the letter b.
  16. Write a pattern that matches the letter g between three and seven times.
  17. Write a program that finds repeated words in an input file and prints the repeated word and the line number on which it was found.
  18. Write a program that uses the translation operator to remove repeated instances of the tab character and then replaces the tab character with a space character.
  19. A rule of good writing is ``avoid the passive voice.'' Instead of The report was read by Carl, say Carl read the report. Write a program that reads a file of sentences (one per line), detects and eliminates the passive voice, and prints the result. (Don't worry about irregular verbs or capitalization, though.)

Sample solution. Sample test sentences.

  1. You have a list of phone numbers. The list is messy, and the only thing you know is that there are either seven or 10 digits in each number (the area code is optional), and if there's an extension, it will show up after an ``x'' somewhere on the line. ``416 555-1212,'' ``5551300X40'' and ``(306) 555.5000 ext 40'' are all possible. Write a fix_phone() sub that will turn all of these numbers into the standard format ``(123) 555-1234'' or ``(123) 555-1234 Ext 100,'' if there is an extension. Assume that the default area code is ``123.''

Sample solution