Thanks for your comments. They are very much to the point and will be well taken ;-). I should definitely avoid putting snippets of code that doens't run on it's own hehe. Promise to improve on that the next time.

Relating to the while loop.. It happened so that I had started with a rather simple one-liner while loop for non-nested tags which worked pretty well. And it happend so that when I thought of adding the nested capability, I simply took that original and added a few boolean clauses to take into account any nested tags (and skip them).

Here's the code as I have it now (slightly modified to run as a stand-alone):
#!/usr/local/bin/perl -w use strict; my $is_nested = 0; my @chunks = ("<cfif bool eq 1>\cJ\cI ", "<cfif foo = bar>\cJ\cI\cI ", "<cfif bar = foo>\cJ\cI\cI ", "</cfif>\cJ\cI ", "</cfif>\cJ\cI\cI \cJ\cI BOOL is true!\cJ", "<cfelse>\cJ\cIBOOL is false!\cJ", '</cfif>'); my $opening_tag = qr/\<cfif/; my $closing_tag = qr/\<\/cfif/; my $found_i = 0; unless ($is_nested) { # search for the closing pair (starting at the place the # first tag was found + 1) # note: this search is good for non-nested tags... # FIRST WHILE while (($chunks[++$found_i] !~ m/^$closing_tag/) && $found_i < @ch +unks) {} } else { # have to search a little differently for nested tags # making sure that an ending tag belonging to # a nested opening tag is not processed as the ending # tag for the current opening tag. # In case i sounded awkward, here's a little diagram: # # <cfif> <--- this tag # <cfif> <--- nested open tag # </cfif> <--- end tag for the nested open tag # </cfif> <--- end tag for this tag (the one that has # to be picked up) # my $nested = 0; # count of nested open tags found. # SECOND WHILE while ((($chunks[++$found_i] =~ m/^$closing_tag/) ? ($nested > 0 ? $nested-- : 0) : ($chunks[$found_i] =~ m/^$opening_tag/ ? ++$nested : +1)) && $found_i < @chunks) { print "F: $found_i ", "N: $nested ", "C: $chunks[$found_i]", ; } }
I, basically, expended this clause
($chunks[++$found_i] !~ m/^$closing_tag/)
found in the first while loop a little bit by adding this
? ($nested > 0 ? $nested-- : 0) : ($chunks[$found_i] =~ m/^$opening_tag/ ? ++$nested : 1).

Certainly, I should agree that in terms of maintainability this may not be a perfect solution. And, therefore, I might have to move that 'logic' inside the while body using a few conditionals (ifs/elses).


"There is no system but GNU, and Linux is one of its kernels." -- Confession of Faith

In reply to Re: (crazyinsomniac) Re: Template Parsing - Finding tag pairs. by vladb
in thread Template Parsing - Finding tag pairs. by vladb

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.