Not quite sure what you are looking for as your code seems to be doing what you want. As per your table of contents, you could push each $line (or [ $heading, $text ]) onto some array @toc whenever you encounter a heading and format it when you are through with all the data.
In reply to Re: Group __DATA__ lines in a while?
by hdb
in thread Group __DATA__ lines in a while?
by Lady_Aleena
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |