misba has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, Just need a quick help on how to read lines from a file in a scenario like this: I need to read a line from a file using below logic assuming FH as file handler for the input file:
while($line=<FH>) { if($line=~ /$SearchString/) { "continue reading lines until I reach the same search string +second time" } }
Please enlighten me with how to continue reading lines from the file inside if condition. Appreciate ur help. Thanks.

Replies are listed 'Best First'.
Re: reading lines from a text file
by jwkrahn (Abbot) on Dec 27, 2008 at 09:07 UTC

    Perhaps this is what you want:

    while ( $line = <FH> ) { if ( $line =~ /$SearchString/ ... $line =~ /$SearchString/ ) { "continue reading lines until I reach the same search string +second time" } }
      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.
        ($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/)
Re: reading lines from a text file
by matrixmadhan (Beadle) on Dec 27, 2008 at 11:30 UTC
    Here is my try using threshold count
    my $threshold = 0; while($line=<FH>) { if($line=~ /$SearchString/) { $threshold++; } last if ( $threshold eq 2 ); # continue processing }
      It should be habit for you to write
      while (my $line = <FH>) { .... }
      because you should always use strict.pm. Right?
        You got that right and I didn't mean to do that.

        I just copied, pasted the sample code posted and modified the version with my logic.

        But anyway, I should have properly given the code snippet.

        Thanks for pointing it out and correcting :)
      thanks guyz for ur replies... i got what i wanted to do.. here is the sample code:
      while($line=<FH>) { print "$line \n"; while(<FH>) { print "Next line is $_ \n"; } }
      Using $_ worked for me in my application. I replaced variable $nextline with $_, so that the inner "while" works with $_ until the condition and $line will continue from where $_ left ( i.e. file handler). Thanks once again guyz. take care and have fun
        Random tips. You should use strict.pm which means you want a my in declaring $line. Also an 8-space indent is too much - in studies comprehension is best with an indent of 2-4 spaces. (There seems to be a marginal benefit to 4 spaces.)

        Sure, these points may look picky. But a good style comes from consistency on trivial details.