in reply to My array element won't print outside a loop

When I run a version of your code that prints inside and outside the loop I get the following result (last few lines only):

Inside >Wonderful Web Servers and Bandwidth Generously Provided by < Inside >pair Networks < Inside > < Inside > < Inside > < Inside > < Inside > < Inside > < Outside > <

Do you really mean to overwrite element 0 of the array with each line in succession in the loop so that only the last line is retained?

use warnings; use strict; use WWW::Mechanize; my $webcrawler = WWW::Mechanize->new(); my $uri = URI->new('http://perlmonks.org/?node_id=482163'); $webcrawler->get($uri); my @stripped_html; my $x = 0; my $content = $webcrawler->content; my $parser = HTML::TokeParser->new(\$content); while($parser->get_tag) { $stripped_html[0] = $parser->get_trimmed_text()."\n"; print "Inside >${stripped_html[0]}<\n"; } print "Outside >${stripped_html[0]}<\n";

Probably what you want is to replace the loop and print with this:

push @stripped_html, $parser->get_trimmed_text()."\n" while($parser->g +et_tag); print join "", @stripped_html;
Update: provide a solution

Perl is Huffman encoded by design.

Replies are listed 'Best First'.
Re^2: My array element won't print outside a loop
by lampros21_7 (Scribe) on Aug 09, 2005 at 14:32 UTC
    Basically i want to write the content of the first link i have into the first element of the array, my array starts from 0 and then i want to check another URL and write that URL's contents into the next element so that would be stripped_html1. Am not quite sure what you get when you run the program with the print statement inside and outside the loop but i think you get what you want when its inside and nothing is written when its outside. I dont want to have an element of the array for every word.Hope this helps.

      broquaint's reply is what you want then. You could write it as:

      $stripped_html[$urlNum] .= $parser->get_trimmed_text() . "\n" while $p +arser->get_tag;

      where $urlNum is incremented for each URL

      Note that the code that I gave in my first reply puts one line in each element - not one word in each element.


      Perl is Huffman encoded by design.