473,406 Members | 2,336 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

While statement in perl

Could someone point me out that why the following perl program only print
'title'. I expect the contents in h1, keywords and description to print out
as well. thanks.

JJL
#!/usr/bin/Perl

use LWP::Simple;
use HTML::TokeParser;
my $content = get( "http://www.perl.com" ) or die $!;
my $stream = HTML::TokeParser->new( \$content ) or die $!;

while ( $tag = $stream->get_tag("title") )
{
my $title=$stream->get_trimmed_text;
print "Title: $title\n";
}
while ( $tag = $stream->get_tag("h1") )
{
my $head1=$stream->get_trimmed_text;
print "Heading1: $head1\n";
}

while ( $tag = $stream->get_tag("meta") )
{
if ($tag->[1]{name}=~/keywords/i)
{
print $tag->[1]{content};
}

if ($tag->[1]{name}=~/description/i)
{
print $tag->[1]{content};
}
}
Jul 19 '05 #1
1 2823
"jjliu" <jj***@earthlink.net> wrote in message news:<v3*******************@newsread1.news.atl.ear thlink.net>...
Subject: While statement in perl
Your question has nothing to do with the while statement in Perl.

The fact that you thought it might indicates you have a problem with
partitioning your problems.

Your question is about HTML::TokeParser.
Could someone point me out that why the following perl program only print
'title'. my $stream = HTML::TokeParser->new( \$content ) or die $!; while ( $tag = $stream->get_tag("title") ) while ( $tag = $stream->get_tag("h1") )


The HTML::TokeParser object is an iterator. Once it reaches the end
it has reached the end. If you want to go back to the beginning you
need to actively do so (by creating a new iterator). HTML::TokeParser
is not unusual in this respect nor is this perculiar to Perl. This is
normal behaviour for iterator objects in pretty much all languages.

Note the get_tags() method can be used to scan for several tag types
in a single pass.

This newsgroup does not exist (see FAQ). Please do not start threads
here.
Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Eric Wichterich | last post by:
Hello Pythonistas, does anyone know an equivalent python statement for perl's "next"-statement? (Perl's "next" skips a loop cycle and continues with the next loop cycle - it does not abort the...
7
by: Xah Lee | last post by:
# here's a while statement in python. a,b = 0,1 while b < 20: print b a,b = b,a+b --------------- # here's the same code in perl
1
by: Xah Lee | last post by:
.. # here's an example of if statement in python. .. .. x=-1 .. if x<0: .. print 'neg' .. elif x==0: .. print 'zero' .. elif x==1: .. print 'one' .. else:
5
by: Xah Lee | last post by:
© # this is an example of for statement © # the % symbol calculates the remainder © # of division. © # the range(m,n) function © # gives a list from m to n-1. © © a = range(1,51) © for x in a:...
3
by: Erica | last post by:
Hi, I'm an experienced programmer, but very new at perl, so please excuse my newbishness :) I am trying to modify an existing piece of perl code for an online store that sells movies. Some of...
3
by: Jeanne | last post by:
I am working on a cgi script that is suppose to pop-up a javascript box from the following perl variables:$TodayDate, $LinkCity, $LinkState. I recently encountered a problem with the $LinkCity...
5
by: crunix | last post by:
Hello. I have developed a medical application with php 4 linked to IBM DB2 database. The database connection is right and i can access data with no problem...but sometimes when i reload the...
9
by: JS | last post by:
#include <stdio.h> main(){ int c, i, nwhite, nother; int ndigit; nwhite = nother = 0; for (i = 0; i < 10; ++i)
11
by: patelxxx | last post by:
When I running the following .cgi script, not sure why each print statement is not printing on a new line, even after entering \n. Browser=IE6 See Perl code below: #!C:/perl/bin/perl.exe -w ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.