my $count = -1; # set to -1 for loops sake and autoincrementing my $placeHolder = 0; # remembers place in array for the first down my $totalDownTime = 0; while ( $count++ <= $#lines ) { if ( $lines[$count] =~ /Down/ ) # finds first down; { $placeHolder = $count; # the following while increments until it finds the next up, # or the end of the array while ( ( $count++ <= $#lines ) && ( $lines[$count] =~ /Down/ ) ) { # do nothing in the while loop } $totalDownTime += sum( $lines[$placeHolder], $lines[$count] ); # sum will be a function defined by you that takes the lines with # down and its corresponding up, parses them, # and returns the time difference } }