Texas A&M Supercomputing Facility Texas A&M University Texas A&M Supercomputing Facility

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)

  1. Change to the Lab2 directory:

    cd /scratch/mylogin/PerlLab/Lab2

    The directory path may be different if you installed the files in another location.
  2. List the directory contents:

    ls -l

    You should see the files: INSTRUCTIONS README Solutions sign.pl stars.pl
  3. 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):
    1. #!/usr/bin/perl
    2.  
    3. # sign.pl - print whether a number is positive, negative, or zero
    4.  
    5. use warnings;
    6. use strict;
    7.  
    8. my $number;
    9.  
    10. print "enter a number: ";
    11.  
    12. $number = <>;
    13.  
    14. # This only tests if a number is negative or not. You need to
    15. # modify the program so it also prints whether it is positive
    16. # or zero.
    17. if ($number < 0)
    18. {
    19. print "Your number is negative\n";
    20. }
    21. else
    22. {
    23. print "Your number is positive\n";
    24. }
  4. 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

  5. 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).
  6. 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

  7. 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.
SOLUTION


Using loops to repeat operations (stars.pl)

  1. 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):
    1. #!/usr/bin/perl
    2.  
    3. # stars.pl - print an increasing number of asterisks on each line, up to 5
    4.  
    5. use warnings;
    6. use strict;
    7.  
    8. my ($i, $j);
    9.  
    10. # write a loop for $i from 1 to 5
    11. #
    12. # print a line with $i asterisks (i.e., when $i is 1, print 1 asterisk,
    13. # when $i is 2, print 2 asterisks, like:
    14. #
    15. # *
    16. # **
    17. # ***
    18. # ****
    19. # *****
    20. #
    21. # suggestion: write a loop with $j from 1 to $i. Inside this loop,
    22. # print an asterisk each time (but no newline). Print the newline
    23. # outside the $j loop.
  2. 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..)
SOLUTIONS