From what I read from your code, your approach is the following:

read through the file "$file" when a starting session is found open the file "$file" again, as file2 read through file2 until the end of the session is found

I would approach the problem differently. I would try to read through the file only once, remembering the start and ending time for each session:

my %session_start; my %session_end; while( <FILE> ) { if( /$pattern/ ) { # Session starts here my $started_session= $1; # actually, you do the counting of pa +rentheses my $session_start_time= $2; # actually, you do the counting of + parentheses if( ! $session_start{ $started_session }) { # That session really is new $session_start{ $started_session }= $session_start_time; } else { # That session should be new but we already know it warn "Line $.: Session '$started_session' already started +at $session_start{ $started_session }, but also (re)starts at $sessio +n_start_time. See [$_]"; }; } elsif( /(\d+-\d+-\d+ \d+:\d+:\d+).*session:(\S{32})/) my $session_end= $1; # actually, you do the counting of parent +heses my $ended_session= $2; # actually, you do the counting of pare +ntheses # Session was seen here, or might end here if( ! $session_start{ $ended_session }) { # That session is unknown?!really is new warn "Line $.: Session '$ended_session' never started, but + ends at $session_end. See [$_]"; } else { # That session was seen here $session_end{ $ended_session }= $session_end; }; } };

Once you have collected all the start and end times for the sessions (and refined your regular expressions so they only match the start/end lines), you just need to output the start/end hashes:

for my $session (sort keys %session_start) { print "Session: $session - $session_start{ $session } - $session_e +nd{ $session }\n"; };

Sanity checking that every session that was started also ends, and that every session that ended also started is left to the user.


In reply to Re: Suspect FH issues by Corion
in thread Suspect FH issues by kdmurphy001

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.