Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^2: [Solved]Need to extract a particular block of lines between two patterns

by haukex (Abbot)
on Nov 09, 2017 at 22:21 UTC ( #1203086=note: print w/replies, xml ) Need Help??


in reply to Re: [Solved]Need to extract a particular block of lines between two patterns
in thread [Solved]Need to extract a particular block of lines between two patterns

Do you see the very-obvious bug in the code? Obviously you do.

Sorry, but I don't. Could you explain or demonstrate the bug with an SSCCE?

There is only one point in time when $count will be exactly equal to 2, and anyone could spot the flaw immediately.

That seems to be the OP's specification, "extract only the second block of lines that meet the pattern" (emphasis mine). Note how the OP's sample input data explicitly says "lines not to be extracted". I've added an extra block to the input for testing:

use warnings; use strict; my $count = 0; while (<DATA>) { if (/START/ .. /END/) { $count++ if /START/; print if ($count == 2); } } __DATA__ abc efg ... START lines NOT to be extracted 1 END START lines to be extracted END START lines NOT to be extracted 2 END START lines NOT to be extracted 3 END

Outputs:

START lines to be extracted END

... which seems to me to meet the specification. Cristoforo's code also works as posted, except that it does not output the START and END markers.

Whereas your code, once I fix all the syntax errors and change "do something with the line" to a print, outputs for the same sample input:

lines to be extracted lines NOT to be extracted 3

Update: Added the last two paragraphs, and minor edits for clarity.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1203086]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (10)
As of 2018-07-17 18:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (374 votes). Check out past polls.

    Notices?