The instructors need help rewriting their gradesheet generation program in Perl.
ID: 3538661 • Letter: T
Question
The instructors need help rewriting their gradesheet generation program in Perl. (It was originally written as a 8-character APL program, and no one alive can figure out how it works;-) ) The requirements are simple:
It should read the file from the filehandle STDIN. so, no opening of files necessary--you can just start reading.
Each line consists of a series of fields, separated by tabs (" ").
The file starts off with a single header line, in which each field is the label for that corresponding column. After splitting the line up into fields at the tabs, scan the list of fields for certain patterns:
There might be other columns, which you should ignore. Also, the columns are in no particular order (except that each "Problem" column has the corresponding comment column immediately after it).
The rest of the file consists of student grades, one student's record per line. Each record contains that student's name, email address, numerical grade for each problem, and a comment for each problem, in the order specified by the headers. As with the headers, any other fields can be safely ignored.
For each student record, you should do the following:
So, here's a toy example. The following table represents the contents of the file, rows representing lines, and columns representing tab-separated values in each line.
Given the input file above, your Perl script should produce the following report:
A few additional remarks and hints:
The input file is guaranteed to be properly formatted, i.e. you don't have to do any error checking, like making sure every line has the correct number of fields, that the grade field is numerical, etc. You also don't have to check for quoted strings, like we did in the in-class example. The whole program should be about 20-25 lines. Keep it simple, and don't try to abuse Perl's many shortcuts. To help you get started, here's a link to the code I wrote during lecture last week to parse quoted strings in tab-separated files similar to the ones for this homework: hw7_perl_example.pl. Also, the short sample file from above is available here: hw7_perl_input.txt
You should name your Perl script "hw7_perl.pl", and submit that file.
Name Problem #1 Comments for P1 E.C. Problem Comments Email Park, A 17 Really bad. 5 park@umbc.edu Doe, A 100 Well done! 0 Why didn't you do this? doe2@umbc.edu Smith, Bob 0 0 smith9999@gmail.comExplanation / Answer
#!/usr/bin/perl $line = ; $i=0; $n=0; $e=0; @probs=(); @hdrs = split(" ", $line); foreach $fld (@hdrs) { if($fld =~ /Name/) { $n=$i; } if($fld =~/Email/) { $e=$i; } if($fld =~ /Problem/) { push(@probs,$i); } $i++; } $f=0; while ($line = ) { chomp($line); $f=0; @flds = split(" ", $line); print "Name: ",$flds[$n]," "; print "Email: ",$flds[$e]," "; foreach $i (@probs) { print "Grade for ",$fld,": ",$flds[$i]," ",$flds[$i +1]; $f=$f+$flds[$i]; if($flds[$i] == 0 && $flds[$i +1] eq "") { print "Not done."; } print " "; } print "Final Grade: ",$f," "; }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.