#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0

    Reading in a .txt file and displaying it properly


    Hello all. I'm really new to Pearl, and I've been working on a solution to this problem. My professor gave several problems (non-graded) in different languages to practice for our final at the end of this week. Only thing is, it's a condensed class and we briefly touched on Pearl, but I'd like to get into it more. Here's the problem (I know it's long):

    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 ("\t").

    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:

    A header field called "Name" indicates the "student name" column.
    A header field called "Email" indicates the "email address" column.
    Any header field that contains the substring "Problem": e.g., "Problem #1" or "Extra Credit Problem", indicates a column containing a grade for that problem. For each of these grade colums, the very next column holds a comment for that grade.

    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:

    Break the line up into fields at tabs.
    Print out "Name: ", followed by the student's name, then a newline.
    Print out "Email: ", followed by the student's email address, then a newline.
    For each column position that corresponds to a problem grade:
    Print out: "Grade for ", then the label for that problem (saved from the header), then the actual numerical grade.
    Then, print out an optional comment according to the following rules:
    If the associated comment is non-blank, print that.
    Else if the grade is 0, print "Not done."
    Otherwise, don't print anything.
    Print out "Final Grade: ", then the sum of the numerical grades.
    End each student's record with a double newline

    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.
    Name Problem #1 Comments for P1 E.C. Problem Comments Email
    Park, John 17 Really bad. 5 park@umbc.edu
    Doe, Jane 100 Well done! 0 Why didn't you do this? doe2@umbc.edu
    Smith, Bob 0 0 smith9999@gmail.com
    Given the input file above, your Perl script should produce the following report:

    Name: Park, John
    Email: park@umbc.edu
    Grade for Problem #1: 17 Really bad.
    Grade for E.C. Problem: 5
    Final Grade: 22

    Name: Doe, Jane
    Email: doe2@umbc.edu
    Grade for Problem #1: 100 Well done!
    Grade for E.C. Problem: 0 Why didn't you do this?
    Final Grade: 100

    Name: Smith, Bob
    Email: smith9999@gmail.com
    Grade for Problem #1: 0 Not done.
    Grade for E.C. Problem: 0 Not done.
    Final Grade: 0

    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. The whole program should be about 20-25 lines. Keep it simple, and don't try to abuse Perl's many shortcuts."

    This is what I have (not very much I know) just to test some things out.

    Code:
     while (<>) {
     chomp;
     ($name, $problem1, $comments1, $ec, $ecomments, $email) = split("\t");
     print "Name: $name\n";
     print "Problem1 $problem1\n";
     print "Comments1: $comments1\n";
     print "EC: $ec\n";
     print "EComments: $ecomments\n";
     print "Email: $email\n";
     print "---------\n";
     }
     exit;
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,940
    Rep Power
    1225
    Do you have a question?

IMN logo majestic logo threadwatch logo seochat tools logo