Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Even this simple code doesn't work as I would expect it to
This means that your expectations are wrong.

A file handle is pretty much an iterator. That implies that it has an internal state. Every time you read from it, from anywhere, does the same thing: it gets the next line from the file, complying to the current internal state, and adjusting it. Yes that means you can read one line from the file in one place in your code, and the next line in another place. The state is in the filehandle, not in the code.

So, first you read everything that is in the file, making the filehandle's internal state point to the end of the file, and then you expect to read even more from the file, and it should, magically, start again from the top??

If perl did that, that would likely be a bit too much of DWIM magic, with a lot of bugs as a result, when you don't want this behavior.

What you need is either to manually reset the filehandle's internal state to start again from the top, as several others have pointed out, using seek; or you must make a clone of the filehandle (so the clone gets its own internal state, a copy from the original) before you read anything from it. For example, you can duplicate the filehandle like this:

open SECONDINSTANCE, "<&INPUTFILE";
but it must also be possible to copy a filehandle into a new one with F_DUPFD. (It's not exactly crystal clear to me how, and I cannot test right now. I'll revisit this node later with an update, unless someone beats me to it first.)

But, you can start by checking out the docs.


In reply to Re: Trying to do multiple while loops on the same input file by bart
in thread Trying to do multiple while loops on the same input file by elef

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-04-23 14:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found