Here's what I've come up with so far (using some of the improvements suggested and benchmarking using GrandFather's example):
my $hdr_search = qr{^\-{4}\s\d};
sub useIndex {
my ($ip1, $ip2, $lines) = @_;
for my $line (@$lines) {
unless (($line =~ $hdr_search) || ($header)) {
next;
}
unless ($line =~ $hdr_search) {
print $line; ## Print matching packet body
next;
}
if (0 > index ($line, $ip1) || (0 > index ($line, $ip2))) {
$header = 0;
next;
} else {
print "\n$line"; ## Print matching packet header
$header = 1;
}
}
}
Using the index function along with getting rid of IF cases early has definitely sped things up considerably. Thoughts?