in reply to Re: Nested div tag
in thread Nested div tag

Thanks for your interest, but when i ran the file nothing assigned to @rows mean $#rows is -1

Replies are listed 'Best First'.
Re^3: Nested div tag
by Anonymous Monk on Jul 21, 2016 at 06:14 UTC

    Thanks for your interest, but when i ran the file nothing assigned to @rows mean $#rows is -1

    What?

      when i run the below code

      #! usr/bin/perl use strict; use warnings; use HTML::TreeBuilder::XPath; my $tree= HTML::TreeBuilder::XPath->new; $tree->parse_file(<<"HTML"); <div class="Row May-2015"> <div class="Cell month-year even"> May-2015 </div> <div class="Cell Release Date even"> 2015-06-25 </div> <div class="Cell National Mortgage Contract Rate even" +> 3.750 </div> </div> HTML my @rows = $tree->findnodes(q{//div[@class= 'Row May-2015']}); #my @rows=$tree->findnodes(q{ //div[ @class =~ /Cell/ ] }); print $#rows; for my $row ( @rows ){ my @cells = $row->findnodes(q{ //div[ @class =~ /Cell/ ] }); for my $cell ( @cells ){ #Dance( $cell ); print $cell; } }

      output be like

      [ab@linux0059 html_tree_builder]$ perl code.pl -1[ab@linux0059 html_tree_builder]$

      It does not take div node

        That is some strange filename

        #!/usr/bin/perl -- use strict; use warnings; use HTML::TreeBuilder::XPath; my $tree= HTML::TreeBuilder::XPath->new_from_content(<<'HTML'); <body> <div class="Row May-2015"> <div class="Cell month-year even"> May-2015 </div> <div class="Cell Release Date even"> 2015-06-25 </div> <div class="Cell National Mortgage Contract Rate even" +> 3.750 </div> </div> HTML $tree->dump; my @rows = $tree->findnodes(q{//div[@class =~ /Row/ ]}); print 0+@rows, "\n"; for my $row ( @rows ){ my @cells = $row->findnodes(q{ //div[ @class =~ /Cell/ ] }); print 0+@cells, "\n"; for my $cell ( @cells ){ print $cell->as_trimmed_text, "\n"; } } __END__ <html> @0 (IMPLICIT) <head> @0.0 (IMPLICIT) <body> @0.1 <div class="Row May-2015"> @0.1.0 <div class="Cell month-year even"> @0.1.0.0 " May-2015 " <div class="Cell Release Date even"> @0.1.0.1 " 2015-06-25 " <div class="Cell National Mortgage Contract Rate even"> @0.1.0.2 " 3.750 " 1 3 May-2015 2015-06-25 3.750