in reply to Re: reading lines from a text file
in thread reading lines from a text file

Thanks for your reply. Let me tell you what I tried and what I want. I tried this:
while ( $line = <FH> ) { if ( $line =~ /$SearchString/) { while(($nextline=<FH>) and ($nextline=~/$SearchString/)) { push @temp $nextline; } #close of inner while } #close of if } #close of while
Now, in the above code, i am trying to read the next line using inner "while" until I reach the search string which I think is not working for me. I want to know whether reading the subsequent lines of file inside outer while loop works or not. If it does not, what is other way of achieving it? Thanks again.

Replies are listed 'Best First'.
Re^3: reading lines from a text file
by Corion (Patriarch) on Dec 27, 2008 at 09:51 UTC
    ($nextline=~/$SearchString/)

    Is it maybe that you have the comparison wrong? You are comparing whether $nextline matches /$SearchString/. I interpret your description as that you want to stay in the loop while $nextline does not match /$SearchString/:

    ($nextline!~/$SearchString/)
      You are right, I want to stay inside inner while loop until nextline matches search string. But lets forget my code, I am just looking for an answer to this:
      while($line=<FH>) { while($nextline=<FH>) { .... } }
      Is this possible, I mean, does the inner "while loop" help me to read next line in the file. How can I read next line in a file, being inside first "while loop". I dont want to break first while loop. Thanks.
        You can answer that for yourself by using
        while($line=<FH>) { print("outer: $line"); while($nextline=<FH>) { print("inner: $nextline"); } print("finished inner\n"); } print("finished outer\n");