in reply to Suspect FH issues
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.
|
|---|