CGS3993, Spring 1999, Perl Assignment 1
Due Tuesday 2/9 at the start of class
Read chapter 2 of the Perl book. Write a PERL script that will do the following
-
Prompt the user and read in two scalar values.
-
Display the values as they were entered, but within double quotes so that
any leading or trailing white space will be identifiable.
-
Use chomp on each scalar and display them again, as above.
-
Display each scalar as a number, and display the sum of the two numbers.
-
Treat the scalars as strings and print their concatenation. (Use the .
operator).
-
Treat the first scalar as a string and the second as a number and print the
first repeated the second number of times. (Use the x operator).
Here is a sample of the output
Please enter something: 12we3
Please enter something else: 3er4
Here is the input
something: "12we3
"
something else: "3er4
"
Here is input after chomp
something: "12we3"
something else: "3er4"
Here is the input treated as numbers
something: "12"
something else: "3"
12 + 3 = 15
Here is the input treated as strings, and concatenated
"12we3" . "3er4" = 12we33er4
Here is the first repeated the second number of times
"12we3" x "3" = "12we312we312we3"
Read chapters 3 and 4 of the Learning Perl book. Write a Perl program
that does the following
-
Reads a list of numbers (on separate lines) until the user enters -1.
-
Prints the list of numbers entered by the user. Do not use a loop for this
part.
-
Prints the list of numbers entered by the user in reverse order. Do not use
a loop for this part.
-
Prints the largest number that the user entered. You will need a loop for
this. Do not use the sort function.
-
Prints the smallest number that the user entered. You will need a loop for
this. Do not use the sort function.
-
Prints the average of the numbers entered.
-
Prints all the numbers that are above the average. You will need a loop for
this.
-
Warning: if the user enters -1 as the first number, then the output should
only say that no numbers were entered. Do not calculate the largest, smallest
and average numbers for this situation.
Where is Perl????
On solix the command for Perl will be
#!/home/sol2/bin/perl
On mongoose the command for Perl will be
#!/usr/local/bin/perl
Creating and Running the program
-
Use either the pico editor or the
emacs editor to type in your program
-
Exit back to the shell, and change the mode of your file to 700
chmod 700 hw2.pl
-
Now, to run the program, just type its name at the prompt. Include ./ before
to be sure the operating system can find it.
./hw2.pl
-
If there are errors, then reopen the editor and make the corrections, then
try to run it again. However, you only have to do the chmod command
once.
Handing it in
-
Be sure that your name, section, and oath attesting that you did the work
on your own are included at the start of your file as comments.
-
To create a listing, use the script command
from solix.
-
Enter the command
script
-
Use the cat command to display the file
cat hw1.pl
-
Run the program several times with different input
-
Repeat steps 2 and 3 for each program.
-
Type exit to end the script session
-
Print out the typescript file using
lpr. Hand in the printout.