Ask a Question related to PERL Modules, Design and Development.

  1. #1

    Default Nested loops?

    Hi,

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

    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]{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/&/&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 );

    =======================================


    DVH Guest

  2. Similar Questions and Discussions

    1. HTML::Template arbitraryily nested recursive loops
      Can anyone suggest a way to display an arbitrarily deep nested loop structure using HTML::Template? Below is what I've tried. I have a data...
    2. Possible Bug w/ nested loops and queries
      May have found a possible bug... can someone please verify? In a database I have two tables, temp1 and temp2 These tables can contain anything,...
    3. Using 'my' within nested loops
      I am curious about the amount of overhead created with the first example below as compared to the second example below: Example 1: my $this;...
    4. [PHP] nested for loops
      your syntax is correct, just need to change for ($j=0: $j < 5; $j++) to for ($j=0; $j < 5; $j++) Anyone ever do a nested for loop? $i =0; $j...
    5. nested for loops
      Anyone ever do a nested for loop? $i =0; $j =0; for ($x=0; $x < 50; $x++){ echo ("1 to 50"); for ($j=0: $j < 5; $j++) { echo ("less than...
  3. #2

    Default Re: Nested loops?

    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?

    usenet@DavidFilmer.com Guest

  4. #3

    Default Re: Tokeparser - Nested loops?


    <usenet@DavidFilmer.com> wrote in message
    news:1131869610.991717.228470@g14g2000cwa.googlegr oups.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?


    DVH Guest

  5. #4

    Default Re: Nested loops?

    "DVH" <dvh@vhvhvhvhvhvhvh.com> 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/&/&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 );
    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.
    Eric Bohlman Guest

  6. #5

    Default Re: Nested loops?

    Nested while loops do not work in Perl...its really amazing that you dnt know it untill you face that problem
    Unregistered Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139