My question is do you see any problem with the code?

Yes. For one, half of it is commented out, and there's waaay too much whitespace. But seriously, maintainability is key (and that is not hidden). I took a little time to get this running, and hopefully i'll be the only one (you should really provide a runnable code example in the future, easier to spot pitfalls ;D). The only thing i'd do different is take this out the while loop (and use a few if and elses here and there).

#!/usr/bin/perl -wl use strict; # 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) # # Actual example: 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; my $nested = 0; # count of nested open tags found. 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]", ; } __END__ F:\dev\vladb>perl nestag.pl F: 1 N: 1 C: <cfif foo = bar> F: 2 N: 2 C: <cfif bar = foo> F: 3 N: 1 C: </cfif> F: 4 N: 0 C: </cfif> BOOL is true! F: 5 N: 0 C: <cfelse> BOOL is false!
update I suspect you'll be building some kind of data structure, and $nested seems like a prime index ;

 
___crazyinsomniac_______________________________________
Disclaimer: Don't blame. It came from inside the void

perl -e "$q=$_;map({chr unpack qq;H*;,$_}split(q;;,q*H*));print;$q/$q;"


In reply to (crazyinsomniac) Re: Template Parsing - Finding tag pairs. by crazyinsomniac
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.