Exercise 2 : Conditionals and loops
Laboratory Exercise for Introduction to Perl
This assumes you have already set up your initial directory with the init-perllab command (instructions).
Testing a condition (sign.pl)
- Change to the Lab2 directory:
cd /scratch/mylogin/PerlLab/Lab2
The directory path may be different if you installed the files in another location. - List the directory contents:
ls -l
You should see the files: INSTRUCTIONS README Solutions sign.pl stars.pl - Look at the sign.pl file:
cat sign.pl
The contents of this file are as follows (note the line numbers have been added for clarity and are not in the actual file):- #!/usr/bin/perl
- # sign.pl - print whether a number is positive, negative, or zero
- use warnings;
- use strict;
- my $number;
- $number = <>;
- # This only tests if a number is negative or not. You need to
- # modify the program so it also prints whether it is positive
- # or zero.
- if ($number < 0)
- {
- }
- else
- {
- }
- Run the sign.pl program and try inputting
various numbers:
./sign.pl
enter a number: 3
Your number is positive
./sign.pl
enter a number: -5
Your number is negative
./sign.pl
enter a number: 0
Your number is positive
- Notice when you input "0" that the
output indicates your number is positive ($number > 0),
which is incorrect. You will need to modifiy this program
so that an input of "0" results in the
answer, "Your number is zero".
Use your preferred editor: vi, emacs, or other. If you do not yet have experience, you may use the editor called "nano".
For reference, read the perl documentation on compound statements (specifically for the "elsif" keyword). - Test your modified program to make sure that it operates
correctly now:
./sign.pl
enter a number: 3
Your number is positive
./sign.pl
enter a number: -5
Your number is negative
./sign.pl
enter a number: 0
Your number is zero
- Note that if the user enters a non-numeric string, such as "hello", the program will give a warning that the program is treating a non-numeric value as a number, but will evaluate the variable to be 0. Later, we will learn how to identify these problems.
Using loops to repeat operations (stars.pl)
- Look at the stars.pl file:
cat stars.pl
The contents of this file are as follows (note the line numbers have been added for clarity and are not in the actual file):- #!/usr/bin/perl
- # stars.pl - print an increasing number of asterisks on each line, up to 5
- use warnings;
- use strict;
- my ($i, $j);
- # write a loop for $i from 1 to 5
- #
- # print a line with $i asterisks (i.e., when $i is 1, print 1 asterisk,
- # when $i is 2, print 2 asterisks, like:
- #
- # *
- # **
- # ***
- # ****
- # *****
- #
- # suggestion: write a loop with $j from 1 to $i. Inside this loop,
- # print an asterisk each time (but no newline). Print the newline
- # outside the $j loop.
- Edit the program so the output is an increasing number of
asterisks ("stars") from 1 to 5 as such:
output
* ** *** **** *****
For reference, read the perl documentation on compound statements (specifically for loop constructs like "for", "while", "foreach", etc..)