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 toThis 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: 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
|
|