CIS 33A Programming in PERL
Assignment D

Please staple the assignment together. At the top of the first page print, in this order: your first name, your last name, CIS33A, and the assignment letter. Print the problem number at the beginning of each problem. Assignments are due at the beginning of class, and will be marked down if turned in later.

Authorship note: These assignments have been adapted from assignments given by Clare Nguyen, who adapted them from the work of Behrouz Forouzan.

Programming Problems are to be done on the computer. Hand in your your source code and program output.

Programming problems

Problem D1

Write a Perl program which opens and reads input from a text file. In this file, each line of text may have multiple occurrences of digit strings.

Your program will extract the digit strings from each line. Repeated occurrences of the same digit string in the same line will be ignored.

Your program will print the unique numbers within each line. Your program will also print the total of these numbers.

Using an input file containing:

99 percent of the time, 99 out of 100 people will not pick the number 1 as the first choice.
A stitch in time saves 9 but only if you have exactly 9.
76 trombones were in 4 marching bands in the big parade of '76 and 76 people attended.

The first line contains the unique numbers: 99 100 1
The second line contains the unique numbers: 9
The third line contains the unique numbers: 76 4
The total is: 289

Your output should look like this:

99 + 100 + 1 + 9 + 76 + 4 = 289

Hint: use the best data structure to make unique entries.

Problem D2

Write a program that allows a user to enter lines of text from the keyboard, which look like this:

San Diego     85

Your script will process input lines as follows:

  1. Reject lines with an improper format by giving an error message and re-prompt.
    If a user enters an empty line (no characters or all white space) simply re-prompt but give no error message.
  2. Format the output lines by removing leading and trailing spaces on the input line,
    by converting multiple white spaces into one space character, and
    by uppercasing all letters on the line to upper case.
  3. Add the formatted line to a scalar string variable via string concatenation.
  4. After the user types "quit", show on screen all valid cities and corresponding temperatures, the number of valid cities entered and the average temperature of these cities.

Using the following input:

NeW    YoRk   CITy   44
San         JOSE  66
  denver   66    44
St.  Louis   55

  Los Angeles   x5
Cupertino
        Anchorage      -2
Death VALLEY    112
	Quit

The Denver, St. Louis, Los Angeles, Cupertino, and Death Valley lines should be rejected.
The output should look like this:

NEW YORK CITY 44
SAN JOSE 66
ANCHORAGE -2
Cities: 3
Average Temperature: 36