CSCI 344
Beginning Perl Scripts
Lab 7


Write the following Perl scripts.  You may use the Internet as a reference for Perl and as a reference for UNIX/Linux commands, but you may not search for solutions to these problems--that would completely defeat the purpose of doing them.

When you complete one of the scripts, notify me and I will come watch a demo and give you credit for completing it.

Here is a link to a good intro to Perl:  perldoc.perl.org/perlintro.html

You may NOT use any UNIX/Linux command or utility to solve these problems.  They can all be solved with basic Perl.

Since you will demonstrate your program to me, your prompts and output don't have to be identical to mine.  I will only look for the same functionality as that in the description.  However, you do need to get the newlines correct.


1)  (weight 1) Write a program that prompts the user for two numbers and then prints the series of numbers <start> .. <end> counting by 2.  

$ count
Enter start:  10
Enter end: 20
10
12
14
15
18
20
$

You will need the chomp function.


2) (weight 1) Write a Perl script that prompts the user for a string and a number N.  Print the string N times.  Print the count on each line.

$ replicate
Enter a string: Perl syntax is better than Bash syntax.
How many times should this string be printed: 4
(1) : Perl syntax is better than Bash syntax.
(2) : Perl syntax is better than Bash syntax.
(3) : Perl syntax is better than Bash syntax.
(4) : Perl syntax is better than Bash syntax.
$


3) (weight 1) Write a Perl script that continually prompts the user for a name and then prints "Hello <name>" on its own line.  If the user presses return without entering any text at the prompt, quit your program.

$ hello
What is your name? Tom
Hello <Tom>
What is your name? Sally
Hello <Sally>
What is your name?
$

You will need to use the chomp function.  Don't forget to make the last character of the prompt a space.



4) (weight 2) Write a Perl script that reads numbers from standard input (until EOF which is ^D at the prompt) and prints the average of all the numbers.  You can do this without an array.

$ average
1
2
3
4
5
^D
3
$

It should also work reading from a file:

$ cat data
1
2
3
4
$ average < data
2.5
$

The defined function is helpful.


5) (weight 3)  Write a function that uses the rand() function to pick a random number.  Modify the results of rand() so that it is always a number between 1-100 (not 0-99, not 1-101).  Prompt the user to guess the number.  If the user's guess is too high, print "too high."   If the user's guess is too low, print "too low."  If the user's guess is correct print that it is correct and print the total number of guesses so far.

$ guess
Guess the number between 1-100: 50
too high
Guess the number between 1-100: 25
too high
Guess the number between 1-100: 12
too low
Guess the number between 1-100: 18
too low
Guess the number between 1-100: 22
Correct!  It took you 5 guesses.
$