COP3832, Fall 1999, Perl Assignment 1
Due Tuesday 9/28 at the start of class
You may run examples of these scripts from this location:
Running Examples. You must have a Java
enabled browser.
Read chapter 2 of the Learning Perl book. Write a Perl script that
does the following. Please create the script on mongoose.
-
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 script
that does the following. Please create the script on mongoose.
-
Read a list of numbers (on separate lines) until the user enters -1.
-
Print the list of numbers entered by the user. Do not use a loop for this
part.
-
Print the list of numbers entered by the user in reverse order. Do not use
a loop for this part.
-
Print the largest number that the user entered. You will need a loop for
this. Do not use the sort function.
-
Print the smallest number that the user entered. You will need a loop for
this. Do not use the sort function.
-
Print the average of the numbers entered.
-
Print 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.
Here is a sample of some output
Enter a list of numbers (-1 to stop)
15
32
12
44
56
16
10
21
21
31
-1
Here is the list of numbers you entered
15
32
12
44
56
16
10
21
21
31
Here is the list of numbers you entered in reverse
31
21
21
10
16
56
44
12
32
15
Here is the largest number: 56
Here is the smallest number: 10
Here is the average: 25.8
Here are the numbers above the average 25.8
32
44
56
31
Read chapter 5 of the Learning Perl book. Write a Perl script that
does the following. Please create the script on mongoose.
-
Create a hash consisting of name and value pairs of your choice. Be sure
there are at least 6 pairs in the hash.
-
Without using a loop, print all the keys of the hash.
-
Without using a loop, print all the values of the hash.
-
Use a loop to print out a list of each pair. Each pair should appear on a
separate line.
-
Delete two elements from the hash.
-
Again, use a loop to print out a list of each pair. Each pair should appear
on a separate line. (If you read ahead, you could write a function).
Here is a sample of the output. Please make up your own hash array.
Here are the keys of the hash
BugsMorkTweetyBillLaverneRalph
Here are the values of the hash
ElmerMindySylvesterBuddyShirleyNorton
Here are the name, value pairs
Bugs Elmer
Mork Mindy
Tweety Sylvester
Bill Buddy
Laverne Shirley
Ralph Norton
Here are the name, value pairs with three pairs deleted
Mork Mindy
Laverne Shirley
Ralph Norton
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 or mongoose.
-
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.
-
Upload a copy of each script via the web:
Submitting Homework Online from mongoose
or solix