If you are a course participant, please send the solutions to these exercises to email@example.com before or on Wednesday March 22, 2000. Note that the only the first ten exercises are obligatory. 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.
In most of these exercises you will be asked to add an extra command to the text processing program texttool. When submitting your results for the first ten exercises, do not send the complete program ten times but send the ten parts that you have written separately. Please note that when a command has more than one text argument like command text1 text2, these may point to the same text (text1 might be the same as text2). Do not forget to let the program check the syntax of the commands before executing them.
Practical tip: when you are entering text after having asked texttool to read your text, you can mark the end of the text by entering Control-Z (Windows) or Control-D (Unix) at the start of a new line.
Add the following function to texttool: count x: counts the lines in text x and shows the result. Example:
> read a test end > count a 2
Add the following function to texttool: grep string x y: selects all lines containing the string string from text x and puts these in text y. The string may not contain white space. Example:
> read b To be or not to be That is the question > grep not b y > print y To be or or not to be
Note added March 15: it is sufficient if this command can handle searching for strings containing characters in the range a-zA-Z0-9
Add the following function to texttool: cat x y: puts text x behind text y. The result is stored in text y. Example:
> read c Line one. > read d Line two. > cat c d > print d Line two. Line one.
Add the following function to texttool: chars x y: divides text x in characters and puts these in text y with each character on a different line. The white space characters in x should be replaced by hash symbols (#). Example:
> read d cr w > chars d y > print y c r # w #
Note added March 15: the final # in the example output was left away by accident until now.
Add the following function to texttool: replace string1 string2 x y: replaces all occurrences of string string1 by string string2 in text x and puts the result in text y. The strings may not contain white space. Example:
> read e He hated hats. > replace hat can e y > print y He caned cans.
Add the following function to texttool: delete x: deletes text x. Example:
> read f This text will disappear. > delete f > print f text variable does not exist
Add the following function to texttool: paste x y z: puts the lines of text y next to those of text x and places the result in text z. The lines should be separated by a single space Example:
> read g When John > read h did met Kate > paste g h z > print z When did John met Kate
Add the following function to texttool: tail number x y: copies the lines of text x to text y starting from the line specified with number. Example:
> read h Will it be raining tomorrow? > tail 3 h y > print y raining tomorrow?
Add the following function to texttool: tokenize x y: divides text x in tokens and puts the result in text y one token per line. You may use the tokenize software you wrote for an earlier session. Example:
> read i John reads. > tokenize i y > print y John reads .
Add the following function to texttool: uniq x: counts how often lines occur in text x and prints the result, with the most frequent lines first. Example:
> read j John won now > chars j y > uniq y 3 n 3 o 2 w 2 # 1 J 1 h
This is a starred exercise which means that you may skip the exercise. Make this exercise only if you think it is interesting and you have some time left.
The commands in texttool require at least one text argument. Modify the program in such a way that when commands are entered without this text argument, a default text is processed. Here is an example:
> read This is a test > tokenize > print This is a test
Implement this default behavior for the commands read and print and at least two of the ten commands you have added.
Add the following function to texttool: alias command1 command2: creates an alias for command command1. From that moment on command1 can be executed by entering command2. Example:
> read l Will aliases work? > alias print show > show l Will aliases work?
It should be possible to create aliases for read, print, the ten added commands and all earlier defined aliases.