-- Regards, Aristotle
Yeah, that wasn't much fun but was hopefully helpful to the fellow.
How's this for fun?...
I had a fragment of HTML that I needed to search for imbalanced tags and then remedy the situation by appending the right number of closing tags.
my ($no, $nc);
for my $t (qw(ol ul b i u a)) {
$no = $frag =~ s/<$t\b/<$t/gi;
$nc = $frag =~ s#</$t\b#</$t#gi;
$frag .= "</$t>" x ($no-$nc) if $no > $nc;
}How about that last line? :) I don't often get to use the '.' operator or the 'x' operator and I thought this was pretty cool.
There are a few flaws with the above approach. It may not fix the incorrect HTML fragment properly but it does protect other HTML in which you insert this fragment. The above method also folds the tags to lower case which may not have been desired. I guess I could have used $& in the replacement portion of th s///.
Comments? Other approaches? Is the s/// operator the only way (of course not!) to get the number of occurences of a regexp in a string?
Jon
