Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: Trying to do multiple while loops on the same input file

by thezip (Vicar)
on Jun 01, 2011 at 16:35 UTC ( #907662=note: print w/replies, xml ) Need Help??

in reply to Trying to do multiple while loops on the same input file

This doesn't answer to your original problem of reading a file with multiple while loops, but it does present another WTDI that, to me, seems much more natural.

Since your file is really small, read it in its entirety into and array of lines, a la:
use strict; use warnings; use autodie; my $filename = 'filename.txt'; open my $ifh, '<', $filename; my @lines = <$ifh>; close $ifh;

Now you can process the content of you file to your heart's content by simply maintaining the relevant indices for the lines you want to be dealing with, and iterating via "for" loops. You can do this as many times as you want without having to worry about iterating via <>...

Updated: Added blurb re: for-loop iteration

What can be asserted without proof can be dismissed without proof. - Christopher Hitchens

Replies are listed 'Best First'.
Re^2: Trying to do multiple while loops on the same input file
by elef (Friar) on Jun 01, 2011 at 17:13 UTC
    Good suggestion. In this particular case, I just moved the open and close commands inside the loop, but in other situations, reading the file into an array would make more sense.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://907662]
[Lady_Aleena]: Now I will see if my perl-fu is as bad as I think it is.
[shmem]: ah, geany uses its own idea of tags. Ah well...
[Lady_Aleena]: My perl-fu is bad.

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2017-04-27 12:37 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (506 votes). Check out past polls.