CMPS 350 Lab 05 - "Introduction to Perl Scripting"

Tech Cultural References:

"The three principal virtues of a programmer are Laziness, Impatience, and Hubris." -- Larry Wall (Perl designer)

From the preface to Programming Perl: "Perl is a language for getting your job done."

resources:
Perl debugger
perl ref
man perlrequick (basic REs)
Perl Code
Read the Perl readme to learn how to run a Perl script. Refer to this basic introduction to Perl while doing this lab. Ignore information on regular expressions, OO, and Perl modules. Refer to the first few pages of this Perl data reference for help on Perl's three built-in data types: scalars, arrays and associative arrays (hashes). Both sets of documentation can be accessed from the command line with:
  man perlintro 
  man perldata
Everything you need to know to complete this lab is in test.perl, spell_check.perl and regex.perl. Copy the files into your working directory so you can test the code:
    $ cp  /home/fac/melissa/public_html/cs350-f15/examples/week05/spell_check.perl .
    $ cp  /home/fac/melissa/public_html/cs350-f15/examples/week05/dictionary .
    $ cp  /home/fac/melissa/public_html/cs350-f15/examples/week05/test.perl .
    $ cp  /home/fac/melissa/public_html/cs350-f15/examples/week05/regex.perl .

Perl uses automatic, dynamic duck-typing. If Perl sees a number when it expects a string, it implicitly converts the number into a string; e.g. 25 becomes "25". If Perl sees a string when it expects a number, it implicitly converts the string into an arbitrary number; e.g. "hello" becomes 0. Data variables are prefaced with a sigil to denote structure: '$' scalar, '@' array, '%' hash. In an assignment the sigil used must match the lvalue type.

Your job is to write a Perl script to process this list of lines. Each line is in this form:

         658,daffy,0,0,1,0,1,1,0,1,1,1 
In each line the first field is ID. The second field is name. The last 10 fields are the results from a quiz where '1' denotes correct and '0' denotes incorrect. Copy the datafile into your working directory:
   
cp  /home/fac/melissa/public_html/cs350-f15/examples/week05/data . 

The usage of your script is:
   lab05.perl [-h] -f <infile> -o <outfile> 

SPECIFICATIONS:

  1. parse the cmdline arguments for required -f and -o switches or optional -h; if -h exit with a usage statement
  2. open 'data' (infile parameter) for reading and 'log' (outfile parameter) for writing (DO NOT HARD CODE FILE NAMES IN PERL)
  3. read file data one line at a time
  4. insert each line from data into an array of lines
  5. in each line use a regex to convert uppercase to lowercase into a field array - 12 fields split on the comma (this array will be overwritten for each new line read)
  6. split the current line from data (or split the line from the array of lines) into a field array
  7. loop through the field array - count the times each question was answered correctly - put this into a count vector (array)
  8. insert each line from data into a hash using field 0 (ID) as key and field 1 (name) as the value
  9. after exiting read file loop print # of lines read for sanity check
  10. close data
  11. prompt user for an ID
  12. query the hash for the ID - respond with value of the key or NOT FOUND
  13. sort the array of lines into a new array
  14. traverse the sorted array - print to log only lines where second digit in ID >= 7 (use a regex - see hint below)
  15. print the correct count vector to log
  16. display highest current count to log
  17. write a function that takes the count array and returns the question number with the highest correct count
  18. close log
The script for this lab can be written in under 100 lines of code - if much longer than this review the sample scripts.

Hint: for your regex to filter the sorted array you can anchor the regex at the beginning of the line using '^'; e.g. /^..[a-z]/ matches any line where the 3rd character is in the set of lowercase letters.

HOW YOUR LAB WILL BE GRADED

The perl script for this lab must reside here:

  /home/stu/{username}/cs350/lab05/lab05.perl  
Sample run:
   $ ./lab05.perl -h
   Usage: lab05.perl [-h] -f <infile> -o <outfile>

   $ ./lab05.perl -f data -o log
   Lines read: 32
   Enter an ID to query: 123
   123 is greasy
   $ cat log
   Lines Sorted by ID and Filtered by ID with 2nd Digit >= 7 
   174,bambi,0,1,1,0,1,0,0,1,1,0
   578,flotsam,1,1,0,1,1,1,1,1,1,0
   688,hugo,0,0,1,0,0,1,0,0,1,0
   799,genie,0,1,1,0,0,1,1,0,1,1
   887,piglet,0,1,1,0,0,1,1,0,1,1
   985,jetsam,1,0,1,0,0,0,0,1,1,1
   Correct Counts:
   #1: 5  #2: 21  #3: 31  #4: 5  #5: 13  #6: 21  #7: 19  #8: 9  #9: 27  #10: 19  
   Question most frequently answered correctly: #3