If your file is huge, then it is probably more of a database job. Otherwise, you could build an index for ALL clients in the first round using tell and then use seek to retrieve those lines in the second round. Whether this extra complexity is justified, would depend on the size of your log file and the number of clients you have.

Update: sample code

use strict; use warnings; my $tell = tell DATA; while(<DATA>){ if( /^(.*?):/ ) { my $client = $1; push @{$lines{$client}}, $tell; $triggered{$client}++ if /trigger/; } $tell = tell DATA; } print "Found triggers for ", join( ", ", keys %triggered ), ".\n"; for( keys %triggered ) { print "Log for client $_:\n"; for( @{$lines{$_}} ) { seek DATA, $_, 0; my $line = <DATA>; print "\t$line"; } } __DATA__ arthur: line 1 arthur: line 2 trigger ford: line 3 # some comment zaphod: line 4 zaphod: line 5 trigger arthur: line 6 ford: line 7

In reply to Re: is this the most efficient way to double-parse a large file? by hdb
in thread is this the most efficient way to double-parse a large file? by jasonl

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.