OK. So you've been hacking about trying everything you can think of, and still it won't lie down. We have all been there. Do not despair. While it's not pretty, your code is nearly there.

Deep breath. Step away from the keyboard.

When lots of things are going wrong, and many appear mysterious, start with the obvious and simple -- fixing those may not fix everything, but at least you can clear away some clutter.

When you start banging your head on the keyboard because some statement doesn't do what you expect, it may be because you are mistaken about what it does, but often it's because the data it's operating on isn't what you expect it to be -- so, one of the obvious and simple things to do is to check the data.

The output you are getting is a mess, there's extra numbers appearing from somewhere, the results don't make sense... But the most obvious thing is that the loop is not stopping... so something must be up with while ($studentname3 ne "END")... which looks as though whatever sets $studentname3 isn't doing its job. You stare at the $studentname3 = substr ($studentname, 0, 3); at the end of the loop, and can see nothing wrong. So... insert a print statement to double check that you're getting what you expect. I'd do something like:

print "\$studentname3='$studentname3'\n" ;
at the end of the loop. If that isn't what you expected... at least you know what to start looking for.

Hint: a number of the monks have pointed towards the line ending problem. The file you are reading contains lines which are exactly 32 characters long, plus the line ending "character", your friend and mine "\n". You know that read (INPUT, $studentname, 20); reads exactly 20 characters from the input. I'm here to tell you that as far as Perl is concerned, the line ending is a character just like any other. Ask yourself, what am I doing with the line endings ?


In reply to Re^4: I'm having a strange bug with basic file input. I'm a total newb. by gone2015
in thread I'm having a strange bug with basic file input. I'm a total newb. by KingCupons

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.