KingCupons has asked for the wisdom of the Perl Monks concerning the following question:

Okay...I have no idea what's wrong here. I've been working on this forever, and can't find a solution to my problem. Oddly, my program follows my teacher's syntax flawlessly. Can any of you guys figure out what's wrong? Here's the program:
open (INPUT, "-Tscore.dat") || die "Cannot open input file.\n"; open (OUTPUT, ">L7AOUT.txt") || die "Cannot open file.\n"; read (INPUT, $studentname, 20); read (INPUT, $test1, 3); read (INPUT, $test2, 3); read (INPUT, $test3, 3); read (INPUT, $test4, 3); $studentname3 = substr ($studentname, 0, 3); print OUTPUT " CLASS TEST AVERAGES\n\n"; print OUTPUT " TEST\n"; print OUTPUT "NAME OF STUDENT AVERAGE\n\n"; while ($studentname3 ne "END") { $avg = ($test1 + $test2 + $test3 + $test4) / 4; $studnum = $studnum + 1; $totavg = $totavg + $avg; printf OUTPUT ("%-24.20s %6.2f", $studentname, $avg); print OUTPUT "\n\n"; read (INPUT, $name, 20); read (INPUT, $test1, 3); read (INPUT, $test2, 3); read (INPUT, $test3, 3); read (INPUT, $test4, 3); $studentname3 = substr ($studentname, 0, 3); } $totavg = $totavg / $studnum; print "\n\n"; printf OUTPUT ("%-25.22s %6.2f", $studnum, $totavg); print "Program completed Successfully."; close INPUT; close OUTPUT; exit 0;
Here's "Tscore.dat":
Tom Thumb 100096093098 Mickey Mouse 088068095086 Minnie Mouse 078056088098 Donald Duck 098096078100 Tad Pole 100100100100 Mack Aroni 095067089098 Cassie Role 082045088079 Mary Martin 085096093088 Mickey Mantle 088063095086 Darryl Strawberry 078056098098 Donald Trump 098086078100 Steve Young 100078097093 Mack Truck 095067099098 Mel Gibson 082075088079 END 000000000000
Here's my Output:
CLASS TEST AVERAGES TEST NAME OF STUDENT AVERAGE Tom Thumb 96.75 Mickey Mouse 532.75 6 Minnie Mouse 555.00 98 Donald Duck 68.00 100 Tad Pole 7.50 0100 Mack Aroni 405.00 89098 Cassie Role 31.75 088079 Mary Martin 129.25 6093088 Mickey Mantl 220.00 63095086 Darryl Stra 19.50 056098098 Donald Tru 2.25 8086078100 Steve You 0.25 00078097093 Mack Tru 0.00 095067099098 Mel Gib 0.00 082075088079 END 0.00 000000000000 0.00 0.00 0.00 0.00 0.00 0.00
...The 0s continue onwards towards infinty...

Any help would be very much appreciated.
My final in this class is at noon. :|

Replies are listed 'Best First'.
Re: I'm having a strange bug with basic file input. I'm a total newb.
by ikegami (Patriarch) on Dec 05, 2008 at 10:44 UTC
    use strict; will find the problem.
Re: I'm having a strange bug with basic file input. I'm a total newb.
by Crackers2 (Parson) on Dec 05, 2008 at 10:49 UTC

    Inside your loop you're reading into $name instead of $studentname. I'm not sure how your sample output still manages to print the correct names, since your printf uses $studentname as well.

    You're also forgetting about the line terminators. The first time you'll correctly read the name and scores. The second time your name will actually contain the line feed from the first line followed by the first 19 characters of the second name (or 18 if it's CR/LF instead of just CR)

    For each subsequent line things will keep shifting. That's probably why you never find the "END".

      Argh...I put in the wrong code up here, then. I've been experimenting so much, and nothing works.
      Even with that fixed, it still doesn't work. I'll change the code. The varnames, anyway...Line terminators? -_-

        Try adding <INPUT>; after both of these lines

        read (INPUT, $test4, 3);

        That will read/skip the linefeed which would otherwise remain on each line.

        (And also fix $name --> $studentname, of course.)

Re: I'm having a strange bug with basic file input. I'm a total newb.
by GrandFather (Saint) on Dec 05, 2008 at 21:13 UTC

    There are many things "wrong", or at least sub-optimum. First, always use strictures (use strict; use warnings;). Those will pick up a whole slew of errors related to typos and brain farts.

    Use the three parameter version of open and lexical variables (open my $inFile, ...) for file handles.

    Use indentation to show the block structure of your code:

    ... while (...) { ... } ...

    You might like to point your teacher to PerlMonks. If your code is a faithful rendition of your teacher's style then the advice above applies to your teacher too!


    Perl's payment curve coincides with its learning curve.
Re: I'm having a strange bug with basic file input. I'm a total newb.
by KingCupons (Initiate) on Dec 06, 2008 at 00:17 UTC
    Okay, I finally got it to work. With a bit of help from you guys, and some help from my teacher.

    I needed to read the "End of Record"...Bleh.


    Thanks everyone!! :)
    # !perl/bin/perl -w #Lab 7a #XXXX XXXX #My full name...Yeah. #Calculates grade averages open (INPUT, "Tscore.dat") || die "Cannot open input file.\n"; open (OUTPUT, ">L7AOUT.txt") || die "Cannot open file.\n"; print OUTPUT " CLASS TEST AVERAGES\n\n"; print OUTPUT " TEST\n"; print OUTPUT "NAME OF STUDENT AVERAGE\n\n"; read (INPUT, $studentname, 20); read (INPUT, $test1, 3); read (INPUT, $test2, 3); read (INPUT, $test3, 3); read (INPUT, $test4, 3); read (INPUT, $eor, 1); <INPUT>; $studentname3 = uc(substr ($studentname, 0, 3)); while ($studentname3 ne "END") { $avg = ($test1 + $test2 + $test3 + $test4) / 4; $studnum = $studnum + 1; $totavg = $totavg + $avg; printf OUTPUT ("%-24.20s %6.2f", $studentname, $avg); print OUTPUT "\n\n"; read (INPUT, $studentname, 20); read (INPUT, $test1, 3); read (INPUT, $test2, 3); read (INPUT, $test3, 3); read (INPUT, $test4, 3); read (INPUT, $eor, 1); <INPUT>; $studentname3 = uc(substr ($studentname, 0, 3)); } $totavg = $totavg / $studnum; print "\n\n"; printf OUTPUT ("%-24.22s %6.2f", $studnum, $totavg); print "Program completed Successfully."; close INPUT; close OUTPUT; exit 0;