#!/usr/bin/perl 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'); my $value = "unicorn"; $i = 0; $newsection = 0; #my $debug = "true"; print "Let's start\n" if $debug; while (my $row = shift(@lines)) { chomp($row); if($newsection <= 0) { if ($row =~ /^[t]/) { $i++; print "New section started\n" if $debug; print "Inserting $row into array\n" if $debug; push(@section,$row); } elsif ($row =~ /^[J]/) { print "Section continued\n" if $debug; push(@section,$row); } elsif ($row =~ /^[S]/) { print "Section continued\n" if $debug; push(@section,$row); } elsif ($row =~ /^[D]/) { print "Section continued\n" if $debug; push(@section,$row); } elsif ($row =~ /^[T]/) { print "Section Ended\n" if $debug; push(@section,$row); $newsection = 1; } } else { $newsection = 0; print "Checking for value\n" if $debug; if ( grep( /$value/i, @section )) { print "Value discovered, saving section\n" if $debug; foreach (@section) { print "$_\n"; } } @section = (); undef(@section); } } exit;