http://www.perlmonks.org?node_id=1042551

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

I have many text files of the same format that I must read. The lines of interest in the text files are between two lines that are consecutive asterisks (************************). What would be an efficient way for me to only consider the lines between these two lines of asterisks?

Replies are listed 'Best First'.
Re: Read Between the Lines
by choroba (Cardinal) on Jul 05, 2013 at 00:21 UTC
    You can use the flip flop operator (see Range Operators):
    while (<>) { if (/^\*+$/ ... /^\*+$/) { print "Inside: $_"; } }
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      I like it. I think the OP didn't want the asterisks themselves printed, so a small addition would clear that up:

      while (<>) { print if /^\*+$/ ... /^\*+$/ and not /^\*+$/; }
Re: Read Between the Lines
by parv (Parson) on Jul 05, 2013 at 00:32 UTC
Re: Read Between the Lines
by rjt (Curate) on Jul 05, 2013 at 01:07 UTC

    If your file is small enough (i.e., not many megabytes in size), slurping the whole thing and using a regexp will be efficient enough, and allow you to capture multiple groups, if necessary, without matching unbalanced asterisks.

    $_ = do { local $/; <> }; print "Match: $_" for /^\*+$ (.+?) ^\*+$/smxg;

    Full example after the <readmore>.

Re: Read Between the Lines
by sundialsvc4 (Abbot) on Jul 05, 2013 at 01:54 UTC

    A more generalized way to handle such requirements is with a Finite-State Machine (FSM) approach.   The algorithms consider, not only the current line of input, but the $state that the FSM is “in” at the time, where the current value of $state is determined by recent history of lines seen.   It is probably overkill for a requirement as trivial as this one seems to be,.