Nested loops?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View


I've been adapting a script which parses HTML and creates an RSS feed
( ).

Now I want to get each URL it parses, then parse the HTML it finds there and
copy part of it to the <description> part of the XML.

I'd like to nest one 'while' loop inside another, as shown below, but it
only picks up the first URL and the trimmed_text, then gives up. Any
pointers would be welcome.


my ($tag, $headline, $url, $description );

while ( $tag = $stream->get_tag("a") ) {

if ($tag->[1] and $tag->[1] eq "docSel-titleLink") {

$url = $tag->[1] || "--";

$headline = $stream->get_trimmed_text('/a');

$url = ' /'.$url;

$url =~ s/\s//g;

$url =~ s/&/&amp;/g;

my $text = get( $url ) or die $!;

my $second_stream = HTML::TokeParser->new( $text ) or die $!;

while ( $tag = $second_stream->get_tag("b") ) {

$description = $second_stream->get_trimmed_text('/b'); }

$rss->add_item( title => $headline, link => $url, description =>
$description );


Re: Nested loops?

DVH wrote:
Quoted text here. Click to load it

Perl never "gives up." What do you mean? Do you mean your script dies
on this line:

Quoted text here. Click to load it

What is the error message it returns?

Re: Tokeparser - Nested loops?

Quoted text here. Click to load it

No error message. It correctly returns the first URL and the trimmed text,
and pushes them into 'link' and 'title'. But until I started trying to get a
second 'while' loop working, it returned twelve URLs and the corresponding
trimmed_text, instead of only one as it does now.

Clearly it finds the first conditions of the loop true, but the remaining
ones false. I'm not sure why?

Re: Nested loops?

Quoted text here. Click to load it

Please show us your actual code, cut-and-pasted rather than retyped (the
code you typed has three opening braces and only one closing brace, so
we're left to guess how your loops are nested).  Please use a reasonable
indentation style and don't double-space it.

Site Timeline