Why don't you check the $_ at the top of your while loop and if it equals "TOTALS" then increment a counter by one. On the line after that you can check the value of the counter...and if it equals whatever number you want to stop at use continue to break the loop?