CSCI 344
Using Perl Arrays
Lab 9

NOTE: March 30 is a school holiday, thus we will not be meeting for lab.  However, you must complete these assignments and demo them to me during the lab on April 6.  This is a hard deadline, I will not accept these problems after April 6.

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.

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 2)  Write a Perl script that reads words from standard input (assume one word on each line).  Count how many words in the input are of length=1, of length=2, and so on.  Print out the lengths and counts.  The Perl function length is helpful.

For example, the following input:

a
at
on
one
cat
dog
computer

would produce the following output

1 1
2 2
3 3
4 0
5 0
6 0
7 0
8 1

I will use ~tyson/344/dict and ~tyson/344/dict.count to test your program.


2) (weight 3) Implement the UNIX wc command.  Your program should read from standard input.  It should work like wc when reading from standard input.  The Perl functions length and split are helpful.

$ wc < file
    5  10   15
$ mywc < file
    5   10   15


3) (weight 5) Implement a postfix calculator that reads an input expression and prints the results.  Handle only the operators + - * /.  Assume (1) that the operands and the operators are delimited using spaces (2) the equation is on a single line (3) the equation is correct.  The Perl function split is helpful.

The stack-based algorithm for solving postfix expressions:  

(1) if the next input is a number, push it onto the stack
(2) if the next input is an operator, pop two elements off the stack, apply the operator, push result

right = pop
left = pop
push(left operator right)
(3) at the end of the input, print the top of stack (stack should contain 1 element, but you don't have to check)

Don't make this harder than it is.  The program is only about 35 lines and should take less than 30 minutes.