in reply to Re: I'm having a strange bug with basic file input. I'm a total newb.
in thread I'm having a strange bug with basic file input. I'm a total newb.

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? -_-
  • Comment on Re^2: I'm having a strange bug with basic file input. I'm a total newb.

Replies are listed 'Best First'.
Re^3: I'm having a strange bug with basic file input. I'm a total newb.
by almut (Canon) on Dec 05, 2008 at 10:56 UTC

    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.)

      Or just drop the read calls altogether and do something like:
      $line=<INPUT>; $studentname = substr($line,0,20); $test1 = substr($line,21,3); ...

      (Though I'm sure a few monks can come up with an unpack solution or a module that handles fixed-width fields)

        Though I'm sure a few monks can come up with an unpack solution
        Here you are =)
        use warnings; use strict; use List::Util qw(sum); my $studnum = 0; my $totavg = 0; while (<DATA>) { my ($name, @tests) = unpack ("A20A3A3A3A3", $_); last if $name eq "END"; my $avg = sum(@tests)/4; $totavg += $avg; $studnum++; printf("%-24.20s %6.2f\n", $name, $avg); } $totavg /= $studnum; printf ("\n\n%-24.22s %6.2f\n", $studnum, $totavg); print "Program completed Successfully."; exit 0; __DATA__ 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


        holli, /regexed monk/
        unpack is better with fixed-width records
        my ( $name, @tests ) = unpack( 'A20 A3 A3 A3 A3', $line );

        With 5.8.0+, you can even do

        my ( $name, @tests ) = unpack( 'A20 (A3)4', $line );

        or

        my ( $name, @tests ) = unpack( 'A20 (A3)*', $line );

        Update: Heh, I hadn't noticed the bit about unpack at the bottom before posting.

      Well, I've tried everything so far...Still no luck. :|
        Confucious says "you must did not try everything then" :)