in reply to Counting the start and end elements in a line

Can anyone tell me what is wrong in this?
quotemeta.

You're probably trying to use special metacharacters for regexes as normal matching characters. BTW you should limit the scope of those variables. And don't use sub prototypes!! (the parens)

You can do this:

sub counttags { my $stag=0; my $etag=0; my $tag = quotemeta($_[0]); while ($_ =~ /<$tag]>/g) {$stag++} while ($_ =~ /<\/$tag>/g) {$etag++} if($stag != $etag) { print "Number of Start and End tags for element $_[0] does not m +atch"; } }
And call you sub with quoted strings, not barewords.

But as a test, this isn't sufficient. You probably should check whether your tags are balanced, not tag soup. I think you'd best use a stack.

sub check_tags_balance { my @tagstack; while(/<(\/)?([^>]+)>/g) { unless($1) { # opening tag push @tagstack, $2; } else { # closing tag unless(@tagstack and $2 eq pop @tagstack) { print "Found an unbalanced closing tag $2\n"; return; } } } if(@tagstack) { print "Missing closing tags for @tagstack\n"; } else { print "Tags balanced.\n"; } } $_ = "I assume <b>everything</b> is <i><b>ok</b></i>"; check_tags_balance();