Another way. I've "corrected" all the presumably incorrect  't13:45\n' lines to  "t13:45\n" (single-quotes to double quotes so the  \n escape means something) and added a blank line to section output for looks.

use strict; use warnings; my @lines = ( "t13:45\n", "D13:45\n", "S13:45 Unicorn\n", "D13:45\n", "S13:45\n" +, "T13:45\n", "t13:45\n", "D13:45\n", "T13:46\n", "t13:45\n", "D13:45\n", "S13:45\n","D13:45\n", "S13:45 UNICORN\n", + "T13:45\n", "t13:45\n", "D13:45\n", "T13:46\n", ); use constant GOOD_LINE => qr{ \A [tTJSD] }xms; use constant END_SECTION => qr{ \A T }xms; my $value = "uNiCoRn"; my @section; LINE: while (my $line = shift @lines) { die "bad line '$line'" unless $line =~ GOOD_LINE; push @section, $line; next LINE unless $line =~ END_SECTION; if (grep m{ (?i) \Q$value\E }xms, @section) { print for @section; print "\n"; } @section = (); } __END__ t13:45 D13:45 S13:45 Unicorn D13:45 S13:45 T13:45 t13:45 D13:45 S13:45 D13:45 S13:45 UNICORN T13:45
The grep in the section-printing conditional at the end of the loop is a little wasteful because it will continue matching after a match is found. See List::Util::any() for an alternative that may be better (because it short-circuits) if the number of lines in a section is very large or matching is very expensive.

Update: It occurs to me that you may want a strict ordering of section markers so that a  't...' line only occurs at the start of a section. If that's the case, define a section start regex
    use constant START_SECTION => qr{ \A t }xms;
and add this die statement
    die "start line '$line' not at start of section"
        if $line =~ START_SECTION and @section;
just before the
    push @section, $line;
statement. That oughta fix it. (Update: And if you want a really strict strict ordering, let me know; can do, will do.)


Give a man a fish:  <%-{-{-{-<


In reply to Re: Cannot find the error (updated) by AnomalousMonk
in thread Cannot find the error by gudmo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.