The following may be what you are looking for, although this allows the match and condition key words to be on the same line. If that's not a requirement the code could be simplified somewhat.

#!/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 = <DATA>)) { 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 looki +ng 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 o +r more lines further into the file but before the next occurrence of a code n +umber. What is the best (most elegant) way to approach this problem? Chet

Prints:

Suppose I want to find something, that is present multiple times in a +large For instance the thing I am looking for might be a set of code numbers + and the
True laziness is hard work

In reply to Re: parsing large files for something by GrandFather
in thread parsing large files for something by cseligman

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.