my @search_space; my $N = 100; foreach my $line () { # fill up @search_space with lines from FILE_B until it has N things # of course, this pukes when FILE_B reaches EOF ;) push(@search_space, ) while (@search_space < $N); # look for $line in @search_space my $pos = find(\@search_space, $line); if ($pos >= 0) { splice(@search_space, 0, $pos); # shift off the stuff in @search_space before the match } else { print "No match for $line"; } } sub find { my ($arr_ref, $find) = @_; $arr_ref->[$_] eq $find and return $_ for (0 .. $#{$arr_ref}); return -1; }