in reply to Template Parsing - Finding tag pairs.
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).
update I suspect you'll be building some kind of data structure, and $nested seems like a prime index ;#!/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!
___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;"
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: (crazyinsomniac) Re: Template Parsing - Finding tag pairs.
by vladb (Vicar) on Dec 25, 2001 at 11:52 UTC |