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 ?
|