Use a tool designed for the job. It looks like you may be generating XML, perhaps by parsing some lightly marked up, sorta HTML, source. So lets go with that for a moment and see what can be done:

use strict; use warnings; use XML::Twig; my $twig = new XML::Twig (pretty_print => 'record'); my $root = new XML::Twig::Elt ('root'); $twig->set_root ($root); my @pendingSections; while (<DATA>) { chomp; if (/<h(\d+)>(.*)/i) { # deal with a header line # First pop any pending sections that need to be closed pop @pendingSections while @pendingSections > $1; while (@pendingSections < $1) { # Add section elements out to current header level my $level = @pendingSections + 1; my $elt = new XML::Twig::Elt ("section$level"); if ($level == 1) { $elt->paste (last_child => $root); } else { $elt->paste (last_child => $pendingSections[-1]); } push @pendingSections, $elt; } my $headElt = new XML::Twig::Elt ("head", $2); $headElt->paste (last_child => $pendingSections[-1]); } else { # It's a paragraph if (@pendingSections) { $pendingSections[-1]->suffix ("$_\n"); } else { $root->suffix ("$_\n"); } } } $twig->print (); __DATA__ <h1>Heading level 1 <h2>Heading level 2 <h3>Heading level 3 <h2>Heading level 2 <h1>Heading level 1 paragraph here paragraph here

Prints:

<root> <section1> <head>Heading level 1</head> <section2> <head>Heading level 2</head> <section3> <head>Heading level 3</head> </section3> <head>Heading level 2</head> </section2> <head>Heading level 1</head>paragraph here paragraph here </section1> </root>

which isn't quite what you showed (especially the root element), but does do the parsing/rendering magic it seems you may be after.


DWIM is Perl's answer to Gödel

In reply to Re: nested level section closing by GrandFather
in thread nested level section closing by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.