#!/usr/bin/perl use strict; use warnings; my %matches = (I => {cond => 'megabyte'}, code => {cond => 'I'},); my $match = "\\b" . join("\\b|\\b", keys %matches) . "\\b"; my %conds; my $condMatch; while (defined(my $line = )) { my @segments = split /(?=$match)/, $line; for my $segment (@segments) { while ($segment =~ /($match)/g) { my $cond = $matches{$1}{cond}; if (exists $conds{$cond}) { delete $conds{$cond}; } else { $conds{$cond} = $line; } $condMatch = join "\\b|\\b", keys %conds; $condMatch = "\\b$condMatch\\b" if $condMatch; } if ($condMatch && $segment =~ /($condMatch)/) { print $conds{$1}; delete $conds{$1}; } } } __DATA__ Suppose I want to find something, that is present multiple times in a large (several hundred megabyte) file. However, I only want it if something else exists after it but before the next occurrence of the thing I am looking for. For instance the thing I am looking for might be a set of code numbers and the condition that I will use to decide whether I want the code may be 1 or more lines further into the file but before the next occurrence of a code number. What is the best (most elegant) way to approach this problem? Chet