in reply to Counting the start and end elements in a line
There are a couple of problems with your sub. You say it takes no parameters with sub counttags() and Perl will enforce that (if it notices at the right time). But you use $_[0] which accesses the first parameter.
You use $_ but don't give it a value anywhere.
The common wisdom is that parsing markup is hard and if it conforms to HTML or XML specifications you are much better to use one of the numerous modules designed for the purpose such as XML::Twig or HTML::TreeBuilder. All that aside, the following clean up of your code and expansion into a runable sample may be a good basis for rephrasing your actual question with some sample code to demonstrate your problem:
use strict; use warnings; my $str = '<start> </begin> </this> </that> <begin> </start>'; counttags ($str, 'begin'); sub counttags { my ($test, $target) = @_; my $stag = $test =~ /<$target>/g; my $etag = $test =~ /<\/$target>/g; if($stag != $etag) { print "Number of Start and End tags for element $target does n +ot match"; } }
Prints:
Number of Start and End tags for element begin does not match
|
|---|