I will try the use strict and use warning. But as far as the $sof_to_delete, it is incrementing at each iteration of the for (@sof_i) statement, but that for (@sof_i) statement has a last if $_=~/^={30}/; statement which should stop the for statement after about 5 lines. (every 5 lines is a line of =========== .