http://www.perlmonks.org?node_id=1043511

Uggles has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to use TreeBuilder to get all of the strings in a certain tag of an HTML document into an array. I use find('mytag') which works fine to get all of the elements, but if I just print the array created from that then it prints HTML::Element hashes instead of the strings. That makes sense considering HTML::Element has the command "as_text". When I try to use "as_text" in the following way:

my @parsed = $element -> find('tag'); -> as_text(); print @parsed;

then it will only print the first instance of text within the desired tag instead of all of them. If I try and use "as_text" on @parsed in the following way:

my @parsed = $element -> find('b'); @parsed -> as_text(); print @parsed;

then I get a "Can't call method 'as_text' without package..." error. Is there a specific syntax for doing this that I just am not getting?

Replies are listed 'Best First'.
Re: HTML::TreeBuilder - Multiple instances of text in the same tag (perlintro)
by Anonymous Monk on Jul 10, 2013 at 23:48 UTC

    Is there a specific syntax for doing this that I just am not getting?

    Yes, read perlintro , esp arrays and foreach