The check for a new section start is in the
$row =~ /^[t]/
condition (the regex is equivalent to /^t/
, BTW). It's located inside the YES-branch of the
if ($newsection <= 0)
but the end of the previous section sets $newsection to 1, so this condition can't be reached.
I'd move the /^t/ condition into the ELSE part of the outermost if, and initialize $newsection with 1 at the very start.
Also, I miss strict and warnings.