COP 3344-01 Introduction to Unix: Lecture 9
Learning objectives
After this class, you should be able to:
- Write simple Perl scripts using the following features: (i) make perl act on each line of a file, (ii) match patterns, (iii) output using
chomp
, (vii) substitute patterns, (viii) use scalar data and variables, (ix) use the default argument$_
, and (x) write conditional statements usingif
andelse
.- Explain the difference between strings that are enclosed in single quotes and those enclosed in double quotes.
Reading assignment
- JEA: section 35.1-35.11.
- Lecture slides: Lecture 9.
Exercises and review questions
- Questions on current lecture's material
- Write a Perl script that will replace all occurrences of either
COP 3344
orCOP3344
withCOP 3345
. Run your script on some sample input file, and store the result in a different output file, using I/O redirection. This feature can be useful, for example, in the following situation. The Computer Science department has a description of courses (see http://www.cs.fsu.edu/current/undergrad/courses.php). If the course number for COP 3344 is changed to COP3345, then this change has to be made throughout the document. Your Perl script can help with this.- Write a Perl script that will prompt the user to input a number, and will output the square of that number.
- Modify the
pprog5
example so that it outputs the record of all those with GPA over 3.5.- Questions on next lecture's material
- Assume that the default variable
$_
contains several fields separated by spaces. Usesplit
to store each field in a separate entry of an array variable namedfields
.