Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^3: I'm having a strange bug with basic file input. I'm a total newb.

by KingCupons (Initiate)
on Dec 05, 2008 at 11:25 UTC ( [id://728262]=note: print w/replies, xml ) Need Help??


in reply to Re^2: 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.

So very true...

Replies are listed 'Best First'.
Re^4: I'm having a strange bug with basic file input. I'm a total newb.
by gone2015 (Deacon) on Dec 05, 2008 at 12:45 UTC

    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 ?

Re^4: I'm having a strange bug with basic file input. I'm a total newb.
by jethro (Monsignor) on Dec 05, 2008 at 12:57 UTC

    Post the updated code if it still doesn't work. Otherwise we can't help because we don't know what you did. But first read on:

    The thing about the line terminators: Every line in a file has a special character at the end that says "Here is the end of the line". Thats the \n. You already use this character when you print lines so you probably know this.

    In your program you read each line, but not the \n at the end. Consequently when you read the second line of the file you have that character at the beginning, you are out of sync with your input file.

    That means you have to read that line ending from the file too even though you don't really need it. You can do that with <INPUT> which you have to put into two places, before the loop after the first line reading and inside the loop after the line reading.

    That is in a lot more words what almut tried to tell you. If you apply that correctly you should see an improvement

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://728262]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-03-28 14:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found