Please help!!!  I'm trying to complete an assignment to determine the number of HTML 
tags within a string at each level of nesting.  ie:  for the string 

"<p><strong>a</strong><a>b</a></p><strong>c<italic>d<a>e</a></italic></strong>"

<p> is at level 0
<strong> and <a> are at level 1
etc...

My code is below.  The problem is that apparently my RegEx is wrong (in the while 
loop), and so the loop is infinite, and no values are printed--just "".  I'm just 
learning perl, and RegEx is really confusing to me.  Any feedback would be appreciated!

Thanks!
Josh


--------begin code--------

#!usr/bin/perl
#Exercise 8.9
#To determine how many HTML tags are included in a string at each level of nesting.

use strict;
use warnings;

my $string = 
"<p><strong>a</strong><a>b</a></p><strong>c<italic>d<a>e</a></italic></strong>";

print("The HTML string is:\n$string\n\n");

our @counter;
count($string, 0);

for (0 .. $#counter){
  print("There are $counter[$_] valid level",$_," html tags.\n");
}

sub count
{
  my $string = shift();
  my $level = shift();

  while ($string =~ m!.*?<([a-z]*?)>(.*)</\1>!g){
#should remember html tag as 1 and contents as 2
    $counter[$level]++;
    print("Tag is \"\1\", contains \"\2\" ");
    print("at level $level\n");
    count($string, $level+1);
#recursively calling count with the string and the level increased by one
  }
}


--------end code--------

Reply via email to