Previous | Home | Lecture Notes | Solutions | Next


Perl Exercises (7)

These exercises are part of a Perl course taught at CNTS - Language Technology Group at the University of Antwerp.

If you are a course participant and you want to submit your answers, please send the solutions to these exercises to before or on Friday March 24, 2000. Note that this exercise collection does not contain obligatory exercises. When you submit your results, please include the Perl code you have written, the result of at least one test and the answers to the questions mentioned in the exercise, if there are any.

Exercise 7.1*

This is a starred exercise which means that you may skip it. Make this exercise only if you think it is interesting and you have some time left.

A filter is a program that reads text from the keyboard, modifies it and prints the result at the screen. A nice feature of filters is that they can be combined. For example:

   DOS> type text | perl filter1 | perl filter2

(replace type with cat if you work on Linux or Unix). Here a file text is send to the first filter. It processes the text and sends the result to a second filter which in its turn processes these results and shows the result at the screen. The vertical lines between the commands are called pipe symbols.

In this exercise your task is to write a filter which selects words based on their position on the line. The required words in the output are specified by numeric arguments of the filter call. The words are separated by sequences of white space. Example:

   DOS> type con | perl filter 2 1 3
   no way
   this is true
    john    hates   mary  and sue
   way no
   is this true
   hates john mary

This command prints the first three words of each line in the order second, first and third. Note: the arguments of a command are stored in the special list @ARGV.

Exercise 7.2*

We provide you with Perl program for playing tic-tac-toe: ttt. Play a few games against it to find out how it works. Your task is to improve the program so that it easily beats our version in a best-of-nine match.

Hint: the program chooses its moves randomly. A version which picks moves in a smarter way will perform better.

Previous | Home | Lecture Notes | Solutions | Next
Last update: March 16, 2000.