Assignment: Perl Scripts

Due Thursday, 10/25/05

1. Calculating Sales

Create a perl script named calcSales that reads an input file specifying sales figures and outputs a tabulation of these figures.

Each line of the input file will be a string of the form name+ or name+sales. The name will not contain any '+' characters. The sales, if present, will be an integer or floating point number (i.e., there may or may not be a decimal point). The name of the input file will be specified on the command line. So, execution of this program might look like this:

perl calcSales.pl salesFigures.txt

The output will consist of a list of names and sales figures, sorted alphabetically by name, that remain after the following processing:

  1. Multiple sales entries for the same name are added.
  2. An entry that does not contain a sales value (e.g.: "Matt Evett+") will erase any entries so far encountered for that name in the data file (but further encounters having a value will "restart" the accumulation for that name with the new value.

You should use a hash to accomplish this. Use the names as keys into the hash, and store the current sales accumulation with that key. Here is a sample input:

John+19
Michael+12
Chunske+10
John+
Michael+3
John+1.52
Bobby+22
Michael+11

And here is a sample output. Note that the accumulated values are aligned:

NAME           SALES
Bobby          23.80
John            4.52
Matt Evett     10.00
Michael        26.00

2. Parsing C Programs

Create a perl script called cparse.pl that counts tokenus in a C program. The name of the program (file) shall be specified via the command line. Thus, the program should be invoked from the command line via:

perl cparse.pl mainProg.c

For each line of the C program cparse.pl should output:

  1. The number of words (variable and reserved) on the line.
  2. The number of numeric literals without decimal points on the line.
  3. The number of numeric literals with decimal points on the line.
  4. The number of braces, brackets and parentheses on the line.

Finally, the script should output the total number of each of the four classes, above, that were in the entire program.

Here is what the end of output might look like:

Words: 8    Ints: 1    Floats: 2    Braces: 4
Words: 3    Ints: 0    Floats: 0    Braces: 2
================================================
TOTALS:
Words: 435  Ints: 23   Floats: 8    Braces: 64

COSC592 Students:

For those of you taking this course as COSC592 (graduate credit), your program must ignore those elements that appear in comments or double quotes. Note that comments can be of either the // or /* ... */ form. The latter form may extend over multiple lines.

Submitting Your Work:

Submit your files to the homework submission system, http://caddis.acad.emich.edu/~hwmatt/student/ (or you can submit a single zip file containing all of them).

Also, don't forget to submit hardcopies of files at the beginning of class, as well.