Nested loops?

Nested loops?

am 12.11.2005 23:07:19 von DVH

Hi,

I've been adapting a script which parses HTML and creates an RSS feed
(http://www.perl.com/lpt/a/2001/11/15/creatingrss.html).

Now I want to get each URL it parses, then parse the HTML it finds there and
copy part of it to the 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]{class} and $tag->[1]{class} eq "docSel-titleLink") {

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

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

$url = 'http://europa.eu.int/rapid/'.$url;

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

$url =~ s/&/&/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?

am 13.11.2005 09:13:31 von usenet

DVH wrote:
> it only picks up the first URL and the trimmed_text, then gives up.

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

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

What is the error message it returns?

Re: Tokeparser - Nested loops?

am 13.11.2005 11:25:14 von DVH

wrote in message
news:1131869610.991717.228470@g14g2000cwa.googlegroups.com.. .
> DVH wrote:
> > it only picks up the first URL and the trimmed_text, then gives up.
>
> Perl never "gives up." What do you mean? Do you mean your script dies
> on this line:
>
> > my $text = get( $url ) or die $!;
>
> What is the error message it returns?
>

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?

am 13.11.2005 17:40:26 von Eric Bohlman

"DVH" wrote in
news:dl5p2m$1fj$1@nwrdmz02.dmz.ncs.ea.ibs-infra.bt.com:

> my ($tag, $headline, $url, $description );
>
> while ( $tag = $stream->get_tag("a") ) {
>
> if ($tag->[1]{class} and $tag->[1]{class} eq "docSel-titleLink") {
>
> $url = $tag->[1]{href} || "--";
>
> $headline = $stream->get_trimmed_text('/a');
>
> $url = 'http://europa.eu.int/rapid/'.$url;
>
> $url =~ s/\s//g;
>
> $url =~ s/&/&/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 );

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.