my ($starttime, $isopen, $totaltime) = (0, 0, 0); while () { if (/open$/) { next if $isopen; $isopen = 1; $starttime = getseconds($_); } elsif (/hold$/ || /closed$/) { next unless $isopen; $isopen = 0; $totaltime += getseconds($_) - $starttime; } }